vbscript namedItem() change select dropdown - html

In Excel VBA change option in the HTML select tag, I used the following code to change options within the <select> tag:
For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
If objOption.Value = SelQ Then
objOption.Selected = True
objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).OnChange
Else
objOption.Selected = False
End If
Next
This seems to work for web sites with nested <table> tags, but the web site was updated without the tags, so, to compensate for finding the selected option, I used this:
For Each objOption In objIE.Document.getElementById("frmProduction").elements.namedItem("WQ").Options
If objOption.Value = strVal Then
objOption.Selected = True
objIE.Document.getElementById("frmProduction").elements.namedItem("WQ").onchange
Exit For
Else
objOption.Selected = False
End If
Next
This is giving me the following error: Run-time error '5002': Application-defined or object-defined error
I used the above solution because it worked in another Internet Explorer application that used <frames> tags, so I modified it a little:
objIE.document.frames("DemographicsIFrame").document.GetElementByID("DropDownPayerID").value = PayerID
objIE.document.frames("DemographicsIFrame").document.GetElementByID("DropDownPayerID").onchange
I've tried to get around it with no success. I can get the selected option to change, but that's it. It won't update the page with required info related to the selected option. In the example above, that's what the onchange event was used for...to change the page contents after the PayerID was updated.
Any advice on how to make this work?

We were actually able to come up with a solution:
For Each objOption In objIE.document.getElementById("frmProduction").elements.namedItem("WQ").Options
If objOption.Value = strVal Then
objOption.Selected = True
Set evtFiroz = objIE.document.createEvent("HTMLEvents")
evtFiroz.initEvent "change", False, True
objIE.document.getElementById("WQ").dispatchEvent evtFiroz
Exit For
Else
objOption.Selected = False
End If
Next

Related

MS Access VBA, efficient way to enable a button only after all required textboxes contains valid data

My code is working, but I just want to know if there is a more efficient way to achieve the same effect.
I have this layout in a form:
In my effort to foolproof the record creation process, I would like to have the "Save and Clear fields" button enabled only after all but the 'Comment' textbox/combobox contains some valid data.
The text/combo boxes are called txtBatteryID, cmbModelNumber, cmbChemistryType, txtSpecVoltage, txtSpecCapacity.
My code is as follow
Private Sub EnableSaveBtnCheck()
'this checks if the required fields contains valid data, if so, enables the save button.
If Me.btnSaveAndCLear.Enabled = False Then
If IsNull(txtBatteryID) = False And IsNull(cmbModelNumber) = False And IsNull(cmbChemistryType) = False And IsNull(txtSpecVoltage) = False And IsNull(txtSpecCapacity) = False Then
Me.btnSaveAndCLear.Enabled = True
End If
End If
End Sub
As you can see, I did the most straightforward way of using AND to combine all must-have conditions in an IF statement. This sub is called in After_Update() event of each text/combo box. Like this:
Private Sub cmbChemistryType_AfterUpdate()
Call EnableSaveBtnCheck
End Sub
My question, in the end, is: Is there a more efficient way to setup the condition "all text/combo box need to have something valid in them"? And is there a more elaborate way to check if the condition is met (something like a event on the form itself)?
Add the values of those 5 fields. If any of them is Null, the sum will be Null. So you only need call IsNull() once.
If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then

Access VBA Set Focus in Column

good afternoon,
I have an access with a subform (sub_frm_robo2) that has a specific column (CD_SENHA).
In the Form Load event I put the code: (Me.sub_frm_robo2.Form! SENHA.InputMask = "Password")
and I am trying to create a condition that at the moment this Column
(CD_SENHA) receives focus (Me.sub_frm_robo2.Form!SENHA.SetFocus),
the data mask be removed (Me.sub_frm_robo2.Form!SENHA.InputMask = "")
and when the focus changes to the next column return the data mask to the initial format (Me.sub_frm_robo2.Form! PASSWORD = "Password")
Below some images to better exemplify
Before Focus
With Focus
After Focus
I think the code would look something like this
Do While Me.sub_frm_robo2.SetFocus = True
If Me.sub_frm_robo2.Form!SENHA.SetFocus Then
Me.sub_frm_robo2.Form!SENHA.InputMask = ""
End If
Next
Can you help me?
Well, I did not have any help here but I managed to resolve after a few attempts and errors, just to code a code in the event of each column of the subformaluario to make it work. Good luck to everyone
Private Sub CNPJ_AF_GotFocus()
Me.Form!SENHA.InputMask = "Password"
End Sub
Private Sub SENHA_GotFocus()
Me.Form!SENHA.InputMask = ""
End Sub

Forms/ VBA - activating controls based on status

This question isn't particularly technical, it's more theoretical.
I'm doing the "project queue" thing in Access that I think we all do from time to time. We've got several steps to go through. I'm hiding buttons that don't make sense in a given step and adding a star to the most relevant tab. For example, if I have been assigned development of a project, the "move to production" button is hidden and disabled because I need to go through testing before I can move the project to production. I also rename the development tab to Development* to queue me to that.
Everything was hardcoded in VBA and it still can be but it's getting longer and uglier to maintain. I'm wondering if someone has a best practice or just general suggestion. I had 6 statuses but I'm going to 12 and feel like it's time to think about doing this better.
When a button is clicked you get a code block like this:
Private Sub AssignScoping_Click()
Me.RequestStatus.Enabled = True
Me.RequestStatus = "Scoping"
Me.RequestStatus.Enabled = False
End Sub
Each button just assigns a different text value to the RequestStatus field which drives the rest of the logic which looks like this:
Private Sub setButtonAvailability()
Select Case Me.RequestStatus
Case Null
Me.PlaceInQueue.Visible = True
Me.PlaceInQueue.Enabled = True
Me.AssignScoping.Visible = False
Me.AssignScoping.Enabled = False
Me.AssignDevelopment.Visible = False
Me.AssignDevelopment.Enabled = False
Me.AssignTesting.Visible = False
Me.AssignTesting.Enabled = False
Me.AssignProduction.Visible = False
Me.AssignProduction.Enabled = False
Me.AssignAutomation.Visible = False
Me.AssignAutomation.Enabled = False
Me.Tabs.Pages("Intake").Caption = "Intake" & "*"
Me.Tabs.Pages("Scoping").Caption = "Scoping"
Me.Tabs.Pages("Development").Caption = "Development"
Me.Tabs.Pages("Testing").Caption = "Testing"
Me.Tabs.Pages("Production").Caption = "Production"
Me.Tabs.Pages("Automation").Caption = "Automation"
...
Case Else
Me.PlaceInQueue.Visible = True
Me.PlaceInQueue.Enabled = True
Me.AssignScoping.Visible = True
Me.AssignScoping.Enabled = True
Me.AssignDevelopment.Visible = True
Me.AssignDevelopment.Enabled = True
Me.AssignTesting.Visible = True
Me.AssignTesting.Enabled = True
Me.AssignProduction.Visible = True
Me.AssignProduction.Enabled = True
Me.AssignAutomation.Visible = True
Me.AssignAutomation.Enabled = True
Me.Tabs.Pages("Intake").Caption = "Intake"
Me.Tabs.Pages("Scoping").Caption = "Scoping"
Me.Tabs.Pages("Development").Caption = "Development"
Me.Tabs.Pages("Testing").Caption = "Testing"
Me.Tabs.Pages("Production").Caption = "Production"
Me.Tabs.Pages("Automation").Caption = "Automation"
End Select
End Sub
I figure there are a ton of options including putting some of the controlling info in a table for just that purpose but thought I'd bounce it off your collective noggins for suggestions as I tend to work in isolation at my job and I don't always think of the best way to do something, just the way I can do it right now.
Textbox and combobox can employ Conditional Formatting rules to set enabled/disabled state.
Doesn't matter what approach is taken, still have to 'touch' each control to set its property. One approach is to generically loop through controls collection without having to explicitly reference each control by name and set the Enabled or Visible properties according to some criteria. Use of control's Tag property might be helpful. Example:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acCommandButton Then
ctl.Visible = ctl.Tag = Me.RequestStatus
End If
Next ctl
Another approach is to give controls similar name, like btnProj1, btnProj2, etc. Then loop could be limited to that set of controls:
For x = 1 to 10
Me.Controls("btnProj" & x).Visible = Me.Controls("btnProj" & x).Tag = Me.RequestStatus
Next
If you set a control not visible, why bother with the Enabled property?

How do you update a selected dropdown value using VBScript

I am trying to problematically change the dropdown selection on an InternetExplorer.Application then save that selection. The code I have so far is
dim myValue
myValue="3"
for j = 0 to obj.Options.length - 1
if(obj.Options(j).Value = myValue) then
obj.Options(j).selected = true
exit for
end if
next
This works on the current pages dropdown list, however when I click save, the value "3" isn't saved and it reverts back to its original value when I reload the page.
Another thing to mention is that when I manually click the dropdown and select a value then save, it does update to the new value when I reload the page. I have tried the obj.click function on it but I do not believe a programmatic mouse click works like a actual mouse click with the action listener.
My guess would be something to do with the databinding between the new value selection and the action listener for the page. I am fairly new to vbscript and have tried all sorts of different things.
Any help would be very much appreciated. Thank You!
Supposing you have the obj object set properly, e.g. something like
set obj = ie.document.getElementById("my_dropdown") then you should ensure that only one option is selected:
for j = 0 to obj.Options.length - 1
if (obj.Options(j).Value = myValue) then
obj.Options(j).selected = true ''' do not exit for
else
obj.Options(j).selected = false
end if
next
or
For Each opt In obj.Options
If opt.Value = myValue Then
opt.Selected = True
Else
opt.Selected = False
End If
Next
Caution: above code snippet could result to (undesired?) case that no option remains selected!

ms access vba code I can disable a field based on content of another field, but my code to re-enable is not working

I have a simple form where I have a field InstrumentTypes_ID and a field DGLenders_ID.
When InstrumentTypes_ID equals "Mortgage" or "Modification", I want DGLenders_ID to stay enabled, but any other value should make DGLenders_ID become disabled.
I have the following code in an AfterUpdate for the InstrumentTypes_ID textBox, which to me seems would be sufficient to get this effect (However, when testing, I can type in "Warranty Deed" (or anything that's not "Mortgage" or "Modification"), which disables the DGLenders_ID field, but if I go back and update it with "Mortgage" or "Modification", it won't re-enable that field):
Private Sub InstrumentTypes_ID_AfterUpdate()
Me.DGLenders_ID.Enabled = True
If Me.InstrumentTypes_ID = "Mortgage" Then
Me.DGLenders_ID.Enabled = True
ElseIf Me.InstrumentTypes_ID = "Modification" Then
Me.DGLenders_ID.Enabled = True
Else
Me.DGLenders_ID.Enabled = False
End If
End Sub
You should try to put your code in a subroutine and call it on the current event of the form and in the lost focus event of the field.