Make hidden field in fill out form visible when selecting option

strooman

Active Member
Joined
Oct 29, 2013
Messages
329
Office Version
  1. 2016
Platform
  1. Windows
While filling out an online form, new hidden fields become visible depending on the options you have previously chosen.

My question is:
Why do these hidden fields become visible while filling out the form manually (selecting and clicking the mouse in real time) but NOT when filling out programmatically?

To replicate, here is the code. Keep in mind that this is a Dutch form. Before you run the code, first start with selecting "Ja" where the question is "Woont u in Nederland?" Then a new field becomes visible. From that point run the code.

The url is:
https://www.belastingdienst.nl/wps/.../hulpmiddel-motorrijtuigenbelasting-berekenen

Code:
Option Explicit

Sub Show_Hidden_Field()
'************************************************************
'Hidden field doesn't unhide when previous option is selected
'Set References to:
'Microsoft HTML.Object Library
'Microsoft Forms 2.0 Object Library
'Microsoft Internet Controls
'Accessible via: Alt+F11 | Tools | References
'************************************************************
    'Declaration section
    Dim IE As New SHDocVw.InternetExplorer
    Dim htmlDoc As New MSHTML.HTMLDocument
    Dim objShell, objShellWindows, objShellWindow As Object
    Dim dd As MSHTML.IHTMLElement
    Dim strVehicle As String
    
    '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 IE 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
                    'Get the content of page
                    Set htmlDoc = objShellWindow.document
                    Exit For
            End If
        End If
    Next objShellWindow
    
    'Find "Vehicle"
    strVehicle = "Personenauto"
    Set dd = htmlDoc.getElementById("V1-2")
    
    If strVehicle = "Personenauto" Then
        dd.Children(1).Selected = True
        '**********************************************************************
        'dd.Children(1).Click                       '<---Tried this option also
        'dd.Children(1).FireEvent ("onchange")      '<---Tried this option also
        
        'set focus to button "Nee"
        htmlDoc.getElementById("V1-1_False").Focus  '<---Tried this option also
        '**********************************************************************
    End If
End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Please confirm which control the code should start with:


  • The yes radio button.
  • The vehicle type drop down select box.
 
Upvote 0
Please confirm which control the code should start with:


  • The yes radio button.
  • The vehicle type drop down select box.

Well, it doesn,t matter much whether it's the "yes" radio button or the car type "personenauto".
It's all about the visibility of the next option. So when i mark "yes" the next dropdown should appear. And when I mark "personenauto" the next dropdown should appear. So how should I code that?
 
Upvote 0
The code below successfully checks the yes button, but nothing happens…
From the internet:

It is worth checking whether checking a box executes any JavaScript on the page.


Code:
Sub Show_Hidden_Field()
'************************************************************
'Hidden field doesn't unhide when previous option is selected
'Set References to:
'Microsoft HTML.Object Library
'Microsoft Forms 2.0 Object Library
'Microsoft Internet Controls
'Accessible via: Alt+F11 | Tools | References
'************************************************************
Dim IE As New SHDocVw.InternetExplorer, htmldoc As Object, dd As Object, objShell, _
objShellWindows, objShellWindow As Object, so As Object
'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 IE 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
            Set htmldoc = objShellWindow.Document   ' get page content
            Exit For
        End If
    End If
Next
Set dd = htmldoc.getElementsByTagName("input")
Set so = dd(2)
MsgBox so.id & vbLf & so.tagName, 64, so.Name
so.Checked = True
End Sub
 
Upvote 0
I have posted the same question also on stackoverflow.com:
https://stackoverflow.com/questions...den-field-visible-after-checking-radio-button

The suggestion for the radio button is to declare it like:
Code:
Dim htmlOption As MSHTML.HTMLInputElement

Then the next inputbox pops up. But when I select "Personenauto", nothing happens. The next box doesn't pop up. Still looking for answer. Like you pointed out, could be a javascript or jquery that must get triggered.
 
Upvote 0
The 'Ja' radio button has a 'change' event which triggers the Jquery. To do the same in VBA IE automation try something like this:

Code:
    Dim HTMLdoc As HTMLDocument
    Dim inputElement As HTMLInputElement
    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
        Dim changeEvent As DOMEvent
    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
        Dim changeEvent As Object
    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

    'After IE page has loaded
    Set HTMLdoc = IE.document
    
    Set changeEvent = HTMLdoc.createEvent("HTMLEvents")
    changeEvent.initEvent "change", True, False

    Do
        Set inputElement = HTMLdoc.getElementById("V1-1_True")
        DoEvents
    Loop While inputElement Is Nothing
    inputElement.Focus
    inputElement.Click
    inputElement.dispatchEvent changeEvent
    DoEvents
The first hidden dropdown also has a 'change' event, so try this to select "Personenauto" and reveal the second hidden dropdown:

Code:
    Dim selectElement As HTMLSelectElement
    Set selectElement = HTMLdoc.getElementById("V1-2")
    selectElement.Focus
    selectElement.Value = "1" 'Personenauto
    selectElement.dispatchEvent changeEvent
    DoEvents
The other hidden dropdowns should work with similar code.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,566
Messages
6,120,262
Members
448,953
Latest member
Dutchie_1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top