VBA: Make hidden field visible after checking radio button - html

I'm trying to fill out a form using VBA (just to get more skilled in programming with VBA). I noticed that the web form is not updating a hidden field when a radio button is checked via VBA.
To be more specific, the hidden field doesn't become visible after checking the radio button.
In my research to why that is I read that: "Events won't (usually) fire when you change the data programmatically". When this assumption is true, what's the difference between filling out a form manually or programmatically? But when this assumption is wrong, what adjustments do I need to make in my code in order to make hidden fields visible after clicking that radio button?
The website I'm working with is from the Dutch Tax authorities:
https://www.belastingdienst.nl/wps/wcm/connect/nl/auto-en-vervoer/content/hulpmiddel-motorrijtuigenbelasting-berekenen
This is my code.
Sub Show_Hidden_Field()
'Declaration section
Dim htmlDoc As New MSHTML.HTMLDocument
Dim objShell, objShellWindows, objShellWindow As Object
Dim htmlOption As MSHTML.IHTMLElement
'Wait an extra 3 seconds so you can switch to the IE window
'and see what's going on
Application.Wait Now + TimeValue("0:00:03")
'Find the correct Internet Explorer window
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each objShellWindow In objShellWindows
If objShellWindow.Name = "Internet Explorer" Then
'Find this window
If objShellWindow.LocationURL = "https://www.belastingdienst.nl/wps/wcm/connect/nl/auto-en-vervoer/content/hulpmiddel-motorrijtuigenbelasting-berekenen" Then
'This the correct page. Get the content of page
Set htmlDoc = objShellWindow.document
Exit For
End If
End If
Next objShellWindow
'Find the radio button . . .
Set htmlOption = htmlDoc.getElementById("V1-1_True")
'. . . and check it.
htmlOption.Checked = True
'**********************************************************************
'I've also tried these options without succes.
'htmlOption.Click
'htmlOption.FireEvent ("onchange")
'Or set the focus to another field
'**********************************************************************
End Sub

You have a couple issues with your code. The first one is that you are using the wrong type for your radio button element.
While htmlOption is technically a MSHTML.IHTMLElement, it more specifically should be of type: MSHTML.HTMLInputElement. So change your declaration to:
Dim htmlOption As MSHTML.HTMLInputElement
For your actual issue, you should just use .Click. Taking a look at developer.mozilla.org:
The HTMLElement.click() method simulates a mouse click on an element.
When click() is used with supported elements (such as an <input>), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events. (emphasis added)
So, you wouldn't need to use .Checked = True, because upon clicking the input element, you will not only solve your problem with the hidden area being revealed, you are also simultaneously changing the value to True.
So you would just use this:
'Find the radio button . . .
Set htmlOption = htmlDoc.getElementById("V1-1_True")
'. . . and check it.
htmlOption.Click

Related

How to add elements revealed by click action to elements list in Excel VBA for an html web form

I'm trying to set up an Excel form that auto-fills an HTML web form. I've figured out how to use VBA to get the elements and cycle through them to add values. My issue is with a text field that is revealed with the click of a check box. I can't have Excel check the box until after I've obtained the elements for the form.
I've looked at the html, and it looks like the field is hidden to some degree when the form is first loaded, as the id and everything can only be found in the tree once the box is checked. The problem is, this field won't show up in the VBA elements list no matter what I try. I've tried re-doing the Set command, and gotten an error when I do that. I'm not sure how to refresh the elements list in VBA to include the new input box.
I used the Set command to get all the elements
Set frm = ie.document.getElementByID("form1")
This is fine, but I can't use that same command to try to re-Set the element list. I get the run-time error 438 (Object doesn't support this property or method)
I tried making a Variant titled frm2, but I get the same error
Sub formFill()
Dim ie As Object
Dim frm As Variant
Dim element As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "THIS IS THE URL"
While ie.readyState <> 4: DoEvents: Wend
'Get form by ID
Set frm = ie.document.getElementByID("form1")
ie.Visible = True
For Each element In frm.elements
Select Case element.Name
Case "fv_RRFC$chkOtherModel"
element.Checked = True
element.FireEvent ("OnClick")
frm.getElementByID("fv_RRFC_txtOtherModel")(0).Value = "test model" 'I tried using the command here, but it didn't work
Case "fv_RRFC$txtRRFC_PROGRAM"
element.Value = "test"
Case "fv_RRFC$txtOtherModel"
element.Value = "test model"
'My attempt to add it to the Select Case. Not surprised this didn't work, as the for Each loop uses the list it had before
End Select
Next
End Sub
I expected to be able to re-load the elements list to interact and fill the newly revealed box, but I've had no luck finding a way to do that.

Press on link inside table with vba on webpage

I figured out how to navigate to the webpage and make it search for my specific input and so fourth. However when i get the list with the result i am not able to make vba click on it. I have tried different variations of:
objIE.document.getElementById("isc_ListGrid_1_body$28s").Click
But I'm unable to figure how to press the specifc cell (marked in blue in pic):
https://imgur.com/inH25WQ
I'm not getting any error's, but its not click on it either.
You want the id of the table, not the id of the div. Then the last td within that table
e.g.
ie.document.querySelector("td:last-child").Click
Try alternatively
Dim evt As Object
Set evt = ie.document.createEvent("HTMLEvents")
evt.initEvent "onclick", True, False
With ie.document.querySelector("td:last-child")
.Focus
.dispatchEvent evt
End With
Or
With ie.document.querySelector("td:last-child")
.Focus
.FireEvent "onclick"
End With

Access Form Bring Text Box in Front of Listbox?

This application is Access 2010, with a SQL backend.
I have a form, which has a tab control, with 2 pages. 1 page has 1 listbox and the other has 2 listboxes. I use some code, on change of the tab control, to determine the active page, and set the listbox(es) row source(s) for the one(s) on the active page, and clear it out on the inactive page. This takes a sec and the listboxes look yucky while this is happening.
To prevent the user from seeing this, I made a text box, with a colored background and the control source ="Please Wait One Moment..."
with like 48 font. On load of the form, this textbox is visible = false. On click of the tab control, the text box is to be made visible, and it is big enough to cover then entire tab control. It does cover the tab control, but I can still see the listboxes through the text box. The text box has a back color and is not transparent. I tried right-clicking the listboxes in design view and choosing Position - Send to Back, and right-clicking the textbox and choosing Position - Send to Front.
This doesn't seem to work. Is anyone familiar with this issue? Figuring someone has tried a trick like this.
The meat of my code is all functioning properly, for this on change of the tab control. I have this code at the beginning:
DoCmd.Hourglass True
Me.txtPleaseWait.Visible = True
Me.Repaint
Application.Echo False
Debug.Print Me.txtPleaseWait.Visible
and this at the end:
Application.Echo True
Me.txtPleaseWait.Visible = False
Me.Repaint
Debug.Print Me.txtPleaseWait.Visible
DoCmd.Hourglass False
Debug.Print "got to end of resting form state"
Does that make sense?
Should I handle this differently?
Thank you.
Edit
I found that listboxes have a higher zorder than textboxes, so I changed my textbox to a listbox. I still see the listboxes from the tab control, through the listbox I'm using to cover them up.
Is there a control that would better cover these and could have this "intermission" type message show, while the listboxes are being set and unset?
I researched different aspects of this, and found the listbox has a higher zorder than text boxes, which is position front or back relative to other objects. I tried just doing this onload of the form, and there wasn't time to hide anything. It just waited to load the form, until the rowsources were set.
Since the find tab is the default one, and quicker to load, I just load that on load. Then, if someone clicks on the other tab, I will load the add listboxes. I also set an integer variable to 0 on load of the form. When I click on the tab to go to the add page (add page is active), I check that variable, and if it is 0, I set the row source, and then +1 to the variable. Next time I click on it, I don't re-order it.
This way, I'm not using resources to load and unload the listboxes, and I only load 2 of them, if the user even goes to that tab. Many times, they might just be looking for one in the system, and going to view it.
Here is the full code, in case this method helps someone else. You could also make the variable boolean, and just set it to true, and then false.
Option Compare Database
Option Explicit
Dim AddLoaded As Integer
Property Get ActivePage() As Access.Page
'PROPERTY TO IDENTIFY WHICH TAB WE ARE ON, FOR FILTERING AND IDENTIFYING WHICH ACTIVE LISTBOX TO LOOK AT, FOR VALUES AND ACTIONS
With Me.tbAddFind
Set ActivePage = .Pages(.Value)
End With
End Property
Private Sub Form_Load()
Dim Listctrl As Control
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Dim cSQL As String
AddLoaded = 0
For Each Listctrl In Form.Controls
If (Listctrl.ControlType = acListBox) Then
With Listctrl
.RowSource = ""
End With
End If
Next Listctrl
Me.tbAddFind.Value = 0
cSQL = "SELECT vw_CMP_Projects.CM_CID, [vw_CMP_Projects]![ProjectName] &" & Chr$(34) & " (" & Chr$(34) & "& [vw_CMP_Projects]![ProjectNo] &" & Chr$(34) & ") " & Chr$(34) & " AS Projects FROM vw_CMP_Projects ORDER BY [vw_CMP_Projects]![ProjectName] &" & Chr$(34) & " (" & Chr$(34) & "& [vw_CMP_Projects]![ProjectNo] &" & Chr$(34) & ")" & Chr$(34)
Me.lstProjects.RowSource = cSQL
Me.lstProjects.Requery
End Sub
Private Sub tbAddFind_Change()
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Select Case Me.ActivePage.Name
Case "pgAddProjects"
If AddLoaded = 0 Then
cmd.ActiveConnection = GetCatalog()
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "sp_RefreshProjectsAdd"
cmd.Execute
Me.lstAllProjects.RowSource = "Select * From qryAddProjectsYes"
Me.lstAllProjects.Requery
Me.lstAddProjects.RowSource = "Select * From qryAddProjectsNo"
Me.lstAddProjects.Requery
AddLoaded = AddLoaded + 1
End If
End Select
End Sub
Thanks for the discussion. It was helpful, as it gave me things to research.

Clicking in IE using VBA

I want to click on a button in IE (which probably is not actually a button) using VBA. The problem is that there is no ID or name in the source of the "button" I want to click on.
Here is the part of the source of the webpage (see link):
Part of source (NEW)
I want to click on the part which has as text: "Informatie" in it. Im currently using the following code (just a part of it) in VBA. But when i excecute the code, nothing happens, not even an error message:
Dim e
Set e = HTMLDoc.getElementsByClassName("BackgroundApp")(0).getElementsByTagName("Div")(7).getElementsByTagName("td")(0)
e.Click
Hard to help without actual page, so I replicate it and here is the tested code:
Dim IE as Object
Dim objCollection As Object
Dim tdNode As Object
Set objCollection = IE.document.getElementsByTagName("td")
For Each tdNode In objCollection
If InStr(1, tdNode.innerHTML, "Informatie", vbTextCompare) > 0 Then
' "Informatie" button is found
tdNode.Click ' click it
Exit For
End If
Next tdNode
Give it a try and report back.

Tab control in VB

I'm working on a legacy VB6 application. I'm sure this probably relates to VB.NET so i will tag it, but please let me know if it's completely different(which I dont think it is) then I'll remove the tag to avoid confusion.
Here is my issue....
I have a Tab control with multiple tabs: 0 - 3. On TabStuff.Tab = 0, I have a few textboxes and comboboxes. The user uses keyboard TAB to move from Indexed controls. What happens is once they get to the last control which is a textbox called txtCity - and click keyboard TAB once more, it brings them to TabStuff.Tab=1.
My issue is I do VALIDATE on txtCity - I call a function that verifies that a couple of the fields aren't NULL and if one of the fields is in fact NULL then I show a MSgBox and try to setFocus on that control. But instead, when OK is clicked on msgbox, it goes to the next tab which is TabStuff.tab=1 which is not correct.
Here's some of my code...
Dim FirstName, City as String
flag=false
firstName = txtName.text
city = txtcity.text
if FirstName="" or isnull(FirstName) then
msgbox "Please enter Name"
tabstuff.tab=0
txtname.setfocus
exit sub
elseif city = "" or isnull(city) then
msgbox "Please enter city"
tabstuff.tab=0
txtcity.setfocus
exit sub
end if
flag=true
This code is in txtCITY_VALIDATE
So in case city was empty, it shows MsgBox, stays on Tab=0 and setfocus on that control, but instead it goes to the next tab=1 and sets focus on the first control of that tab.
EDIT:
in txtCITY_LostFocus
If Flag = False Then
TabStuff.Tab = 0
Exit Sub
End If
I added this but it still goes to tabstuff.tab=1 setting the focus on first control of the tab
EDIT 2:
In a new project i created txt1 and txt2 - i set TabIndex 0 and 1 respectively.
Private Sub Txt1_Validate(Cancel As Boolean)
If Txt1.Text = "" Then
MsgBox "no text in txt1"
Txt1.SetFocus
End If
End Sub
This is the code I use. I click TAB on txt1 without entering any text, so this gets executed, but after msgbox, the focus gets set on txt2
For some extremely weird reason - i seem to have been getting this discrepancy because I was doign it in the VALIDATE property. When i entered the same code in LostFOCUS it seems to work fine. Thanks everyone for your help with this!