Link to home
Start Free TrialLog in
Avatar of Sanjay
SanjayFlag for United States of America

asked on

ms access vba code to auto query and fill input boxes on a Internet Explorer web form

I understand MS Access vba code within MS Access but not familiar with interfacing with other applications such as internet explorer.  Trying to automate user tasks.  I have developed code based on internet research and my close but not there.

This is what the user currently has to do and what we are trying to accomplish:

1.  Fill out the Container field with a text string (need to automate the pass of this value); I have done this in code below

2.  Press RETURN on keyboard which then reloads the web form to show a drop down list for the Mfg Order field.  This list has checkboxes on the left side with corresponding text values on the right side (need to refresh this reload); don't know how to do this and this appear to be the crutch of solving the problem in order to get to 3. below.

3.  The user then clicks only one checkbox of interest (need to automate the pass of this value)

4. Click on the ViewReport button (need to automated this)

Eg of Container value is "07016216" (this value is captured from my MS Access Form; just hardcoded below)
Eg of Mfg Order value to select is "ZMCSVC177759" (remember, this drop down list can have many values which the user has to pick only one from the drop down list; this value is captured from my MS Access Form; just hardcoded below)


Many thanks!!!!!



Code below:

Public Function cam()
     
     'make sure you add references to Microsoft Internet Controls (shdocvw.dll) and
     'Microsoft HTML object Library.
     'Code will NOT run otherwise.
     
    Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
    Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
    Dim htmlInput As MSHTML.HTMLInputElement
    Dim htmlColl As MSHTML.IHTMLElementCollection
    Dim IE As InternetExplorer
    Dim URL As String
   
    URL = "http://zlvcsssrs/Reports_CAM_GMMA_PRODRPT/Pages/Report.aspx?ItemPath=%2fServiceSummaryReports%2fRAServiceSummaryReport"

     
    'Set objIE = New SHDocVw.InternetExplorer
    Set IE = New InternetExplorer
     
    'With objIE
    With IE
        .Navigate URL     'Main page
        .Visible = 1
        Do While .Busy: DoEvents:   Loop
        Do While .ReadyState <> 4: DoEvents: Loop
        Set htmlDoc = .Document
        Set htmlColl = htmlDoc.getElementsByTagName("input")
        For Each htmlInput In htmlColl
            If htmlInput.Name = "ctl32$ctl04$ctl03$txtValue" Then
                Call htmlDoc.GetElementByID("ctl32_ctl04_ctl03_cbNull").SetAttribute("checked", False)
                Call htmlDoc.GetElementByID("ctl32_ctl04_ctl03_txtValue").SetAttribute("disabled", False)
                htmlDoc.GetElementByID("ctl32_ctl04_ctl03_txtValue").Focus
                htmlInput.Value = "07016216"
                htmlDoc.GetElementByID("ctl32_ctl04_ctl03_txtValue").Focus
                While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
                    DoEvents
                Wend
                Exit For
            End If
        Next htmlInput
               

     '<select name="MfgOrder">
'    Dim optionIndex As Integer
'    Dim dirSelect As HTMLSelectElement
'    Set dirSelect = htmlDoc.getElementsByName("ctl32$ctl04$ctl05$ddDropDownButton")(0)
    'dirSelect.selectedIndex = 1            'set option index directly
'    optionIndex = Find_Select_Option(dirSelect, "ZMCSVC177759")
'    If optionIndex >= 0 Then
'        dirSelect.selectedIndex = optionIndex
'    End If


'        For Each htmlInput In htmlColl
            If htmlInput.Name = "ctl32$ctl04$ctl05$txtValue" Then
'                'Call htmlDoc.GetElementByID("ctl32_ctl04_ctl03_cbNull").SetAttribute("checked", False)
'                'Call htmlDoc.GetElementByID("ctl32_ctl04_ctl03_txtValue").SetAttribute("disabled", False)
                htmlDoc.GetElementByID("ctl32_ctl04_ctl05").Focus
                htmlInput.Value = "ZMCSVC177759"
                htmlDoc.GetElementByID("ctl32_ctl04_ctl05").Focus
'                Exit For
            End If
'        Next htmlInput
                 
'                Set htmlDoc = .Document
'                Set htmlColl = htmlDoc.getElementsByTagName("a")
'                Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
'                    For Each htmlInput In htmlColl
'                        If Trim(htmlInput.ID) <> "" Then
'                            If htmlInput.ID = "ctl00_ctl16_SearchButton" Then
'                                htmlInput.Click
'                                Exit For
'                            End If
'                        End If
'                    Next htmlInput
'
'                    Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
'                        MsgBox "New URL is : " & objIE.LocationURL

    While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
        DoEvents
    Wend
                         
    End With
   
    'Set objIE = Nothing
                     
End Function
Capture.PNG
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

I will take a wild guess but this Web form you are trying to fill is probably from an application that is owed by your company so it's full "accessible" ...so instead trying to automate IE wouldn't be easier to connect directly to the underlying database and insert the data...?
Avatar of Sanjay

ASKER

Hi John:

Yes this is our company's form.  This web form acts as a reporting tool only (SSRS) where the user enters a value, then selects one item from the drop down box, clicks on the "View Report" and can then view the report on the web form.  The user (end-user) cannot connect to the underlying database and cannot insert data.

Thanks!

SG
Avatar of Sanjay

ASKER

I inspected the Container field element on the web form and this is what I have:

<input name="ctl32$ctl04$ctl03$txtValue" class="null" id="ctl32_ctl04_ctl03_txtValue" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'ctl32$ctl04$ctl03$txtValue\',\'\')', 0)" type="text" size="30">

So I have to basically submit the form with a value in the Container, which I can fill in programmatically, but do not know how to send the form for processing.  After processing, the Mfg Order list is populated, from which I have to click a checkbox in the drop down to select the Mfg Order, and then I have to click View Report.
Avatar of Sanjay

ASKER

Ok, I added the following for the Container field, which works such that the Mfg Order list box get populated; whew...getting there.

htmlDoc.parentWindow.execScript "__doPostBack('ctl32$ctl04$ctl03$txtValue\',\'\')','')"

Now just need help in looping through the listbox and  "selecting" the correct Mfg Order (I have it hardcoded right now)...


Thanks!

Sanjay
Avatar of Sanjay

ASKER

For the combo box click button (id: ctl32_ctl04_ctl05_ddDropDownButton)

I added this

xOffset = SetSelect(htmlDoc.getElementById("ctl32_ctl04_ctl05_ddDropDownButton"), "ZMCSVC177759")
doc.getElementById("ctl32_ctl04_ctl05_ddDropDownButton").selectedIndex = xOffset

which calls the SetSelect function below (the idea here is to find the value ZMCSVC177759 from the drop down combo box list); but the code fails at:

For x = 0 To selectElement.Options.Length - 1  (the error states:  object doesn't support this property or method); Maybe I should call the listbox's id and not the dropdownbutton.........



Function SetSelect(selectElement As HTMLSelectElement, xComboValue) As Integer
    'Finds an option in a combobox and selects it.

    Dim x As Integer

    For x = 0 To selectElement.Options.Length - 1
        If selectElement.Options(x).Text = xComboValue Then
            selectElement.selectedIndex = x
            Exit For
        End If
    Next x

    SetSelect = x

End Function
Avatar of Sanjay

ASKER

I tried using the list box's ID and still got the same error.........

Could use some help:)

SG
Avatar of Sanjay

ASKER

Here is the element for the combo box:

<input name="ctl32$ctl04$ctl05$txtValue" class="null" id="ctl32_ctl04_ctl05_txtValue" type="text" size="28" readOnly="readonly">



Here is the element for the combo box button:

<input name="ctl32$ctl04$ctl05$ddDropDownButton" title="Select a value" id="ctl32_ctl04_ctl05_ddDropDownButton" style="margin-top: 1px; vertical-align: top; cursor: pointer;" type="image" alt="Select a value" src="/Reports_CAM_GMMA_PRODRPT/Reserved.ReportViewerWebControl.axd?OpType=Resource&amp;Version=11.0.3000.0&amp;Name=Microsoft.Reporting.WebForms.Icons.MultiValueSelect.gif">
Avatar of Sanjay

ASKER

Ok, found the element for the checkbox within the drop down list as follows:

<input name="ctl32$ctl04$ctl05$divDropDown$ctl02" id="ctl32_ctl04_ctl05_divDropDown_ctl02" onclick="$get('ctl32_ctl04_ctl05').control.OnValidValueClick(this, '');" type="checkbox" CHECKED="checked">

Now my question is how to I select the one choick from the many list of checkboxes against what I prefilled in the listbox?
Avatar of Sanjay

ASKER

Ok this is what I see for the drop down box AND the selected field (the label shows the value of the mfg order: ZMCSVC177759:; So how do I capture this value that I can then compare against a preset value?  I think I am getting there...........

<input name="ctl32$ctl04$ctl05$divDropDown$ctl0
    2" id="ctl32_ctl04_ctl05_divDropDown_ctl02" onclick="$get('ctl32_ctl04_ctl05').control.OnValidValueClick(this, '');" type="checkbox" CHECKED="checked">

    <label for="ctl32_ctl04_ctl05_divDropDown_ctl02">ZMCSVC177759</label>
    This question needs an answer!
    Become an EE member today
    7 DAY FREE TRIAL
    Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
    View membership options
    or
    Learn why we charge membership fees
    We get it - no one likes a content blocker. Take one extra minute and find out why we block content.