Sanjay
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.IHTMLElementCollect ion
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.getElementsByTagNa me("input" )
For Each htmlInput In htmlColl
If htmlInput.Name = "ctl32$ctl04$ctl03$txtValu e" Then
Call htmlDoc.GetElementByID("ct l32_ctl04_ ctl03_cbNu ll").SetAt tribute("c hecked", False)
Call htmlDoc.GetElementByID("ct l32_ctl04_ ctl03_txtV alue").Set Attribute( "disabled" , False)
htmlDoc.GetElementByID("ct l32_ctl04_ ctl03_txtV alue").Foc us
htmlInput.Value = "07016216"
htmlDoc.GetElementByID("ct l32_ctl04_ ctl03_txtV alue").Foc us
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$ctl 04$ctl05$d dDropDownB utton")(0)
'dirSelect.selectedIndex = 1 'set option index directly
' optionIndex = Find_Select_Option(dirSele ct, "ZMCSVC177759")
' If optionIndex >= 0 Then
' dirSelect.selectedIndex = optionIndex
' End If
' For Each htmlInput In htmlColl
If htmlInput.Name = "ctl32$ctl04$ctl05$txtValu e" Then
' 'Call htmlDoc.GetElementByID("ct l32_ctl04_ ctl03_cbNu ll").SetAt tribute("c hecked", False)
' 'Call htmlDoc.GetElementByID("ct l32_ctl04_ ctl03_txtV alue").Set Attribute( "disabled" , False)
htmlDoc.GetElementByID("ct l32_ctl04_ ctl05").Fo cus
htmlInput.Value = "ZMCSVC177759"
htmlDoc.GetElementByID("ct l32_ctl04_ ctl05").Fo cus
' Exit For
End If
' Next htmlInput
' Set htmlDoc = .Document
' Set htmlColl = htmlDoc.getElementsByTagNa me("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
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.IHTMLElementCollect
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.getElementsByTagNa
For Each htmlInput In htmlColl
If htmlInput.Name = "ctl32$ctl04$ctl03$txtValu
Call htmlDoc.GetElementByID("ct
Call htmlDoc.GetElementByID("ct
htmlDoc.GetElementByID("ct
htmlInput.Value = "07016216"
htmlDoc.GetElementByID("ct
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(
'dirSelect.selectedIndex = 1 'set option index directly
' optionIndex = Find_Select_Option(dirSele
' If optionIndex >= 0 Then
' dirSelect.selectedIndex = optionIndex
' End If
' For Each htmlInput In htmlColl
If htmlInput.Name = "ctl32$ctl04$ctl05$txtValu
' 'Call htmlDoc.GetElementByID("ct
' 'Call htmlDoc.GetElementByID("ct
htmlDoc.GetElementByID("ct
htmlInput.Value = "ZMCSVC177759"
htmlDoc.GetElementByID("ct
' Exit For
End If
' Next htmlInput
' Set htmlDoc = .Document
' Set htmlColl = htmlDoc.getElementsByTagNa
' Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
' For Each htmlInput In htmlColl
' If Trim(htmlInput.ID) <> "" Then
' If htmlInput.ID = "ctl00_ctl16_SearchButton"
' 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
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...?
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
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
ASKER
I inspected the Container field element on the web form and this is what I have:
<input name="ctl32$ctl04$ctl03$tx tValue" class="null" id="ctl32_ctl04_ctl03_txtV alue" onkeypress="if (WebForm_TextBoxKeyHandler (event) == false) return false;" onchange="javascript:setTi meout('__d oPostBack( \'ctl32$ct l04$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.
<input name="ctl32$ctl04$ctl03$tx
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.
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.execS cript "__doPostBack('ctl32$ctl04 $ctl03$txt Value\',\' \')','')"
Now just need help in looping through the listbox and "selecting" the correct Mfg Order (I have it hardcoded right now)...
Thanks!
Sanjay
htmlDoc.parentWindow.execS
Now just need help in looping through the listbox and "selecting" the correct Mfg Order (I have it hardcoded right now)...
Thanks!
Sanjay
ASKER
For the combo box click button (id: ctl32_ctl04_ctl05_ddDropDo wnButton)
I added this
xOffset = SetSelect(htmlDoc.getEleme ntById("ct l32_ctl04_ ctl05_ddDr opDownButt on"), "ZMCSVC177759")
doc.getElementById("ctl32_ ctl04_ctl0 5_ddDropDo wnButton") .selectedI ndex = 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.Leng th - 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.Leng th - 1
If selectElement.Options(x).T ext = xComboValue Then
selectElement.selectedInde x = x
Exit For
End If
Next x
SetSelect = x
End Function
I added this
xOffset = SetSelect(htmlDoc.getEleme
doc.getElementById("ctl32_
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.Leng
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.Leng
If selectElement.Options(x).T
selectElement.selectedInde
Exit For
End If
Next x
SetSelect = x
End Function
ASKER
I tried using the list box's ID and still got the same error.........
Could use some help:)
SG
Could use some help:)
SG
ASKER
Here is the element for the combo box:
<input name="ctl32$ctl04$ctl05$tx tValue" class="null" id="ctl32_ctl04_ctl05_txtV alue" type="text" size="28" readOnly="readonly">
Here is the element for the combo box button:
<input name="ctl32$ctl04$ctl05$dd DropDownBu tton" title="Select a value" id="ctl32_ctl04_ctl05_ddDr opDownButt on" style="margin-top: 1px; vertical-align: top; cursor: pointer;" type="image" alt="Select a value" src="/Reports_CAM_GMMA_PRO DRPT/Reser ved.Report ViewerWebC ontrol.axd ?OpType=Re source& ;Version=1 1.0.3000.0 &Name= Microsoft. Reporting. WebForms.I cons.Multi ValueSelec t.gif">
<input name="ctl32$ctl04$ctl05$tx
Here is the element for the combo box button:
<input name="ctl32$ctl04$ctl05$dd
ASKER
Ok, found the element for the checkbox within the drop down list as follows:
<input name="ctl32$ctl04$ctl05$di vDropDown$ ctl02" id="ctl32_ctl04_ctl05_divD ropDown_ct l02" onclick="$get('ctl32_ctl04 _ctl05').c ontrol.OnV alidValueC lick(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?
<input name="ctl32$ctl04$ctl05$di
Now my question is how to I select the one choick from the many list of checkboxes against what I prefilled in the listbox?
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$di vDropDown$ ctl0
ropDown_ct l02" onclick="$get('ctl32_ctl04 _ctl05').c ontrol.OnV alidValueC lick(this, '');" type="checkbox" CHECKED="checked">
<label for="ctl32_ctl04_ctl05_div DropDown_c tl02">ZMCS VC177759</ label>
<input name="ctl32$ctl04$ctl05$di
<label for="ctl32_ctl04_ctl05_div
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.