Entering data using VBA in to a HTML5 site - html

Hi i need to enter a car reg into the below input box:
<input type="text" class="form-control text-uppercase car-registration__input ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength ng-touched"
data-ng-model-options="{updateOn: 'blur'}"
data-ng-keyup="inputChange=true; regnumberNotFound=false; carLookupButtonSubmitted=false" data-motor-regnumber=""
data-msm-answer-store-key="" data-ng-required="true"
data-msm-answer-store-action="{'regnumberUpdate': {'targets': [{}]}}"
data-msm-error-tracking-view-change="" id="regnumber" name="regnumber"
data-ng-model="regnumber" maxlength="10" data-msm-field-interaction-events="click"
data-ng-change="returnedUserCheck()" required="required">
using the following i can enter the value but the box doesn't think that there is anything there and therefore wont validate and i have to manually click inot the text box:
IE.document.all("regnumber")(1).innerText = Reg
IE.document.all("regnumber")(1).setCapture
IE.document.all("regnumber")(1).Click
IE.document.all("regnumber")(1).releaseCapture
IE.document.getElementsByTagName("BUTTON")(7).Click
Any help would be greatly appreciated

Try replacing:
IE.document.all("regnumber")(1).innerText = Reg
With:
IE.document.all("regnumber")(1).value = Reg
The value attribute specifies the value of an <input> element
The value attribute is used differently for different input types:
For "button", "reset", and "submit" - it defines the text on the
button
For "text", "password", and "hidden" - it defines the initial
(default) value of the input field
For "checkbox", "radio", "image" -
it defines the value associated with the input (this is also the value
that is sent on submit)
Note: The value attribute cannot be used with <input type="file">.
Edit:
It looks as if it's using angular to check the value has changed.
Specifically, this attribute on the input element:
data-ng-keyup="inputChange=true; regnumberNotFound=false; carLookupButtonSubmitted=false"
It appears that you would need to also trigger a keyup event, which will likely require something like the following:
IE.document.all("regnumber")(1).Focus()
IE.document.all("regnumber")(1).Value = Reg + "." 'extra full stop will be removed on the next line
SendKeys "{BS}"
However, SendKeys is usually frowned upon unless absolutely necessary.
It may be worth trying to "trick" the form to recognise the value change by forcing the blur event to occur.
This can be done by setting the focus to the input box, then setting the focus to another element, as follows:
IE.document.all("regnumber")(1).Focus()
IE.document.all("regnumber")(1).innerText = Reg
IE.document.getElementsByTagName("BUTTON")(7).Focus()
IE.document.getElementsByTagName("BUTTON")(7).Click
I'd recommend trying this suggestion before going down the SendKeys route

I was having similar trouble with a form and I found this:
VBA interaction with internet explorer
Which helped me solve the exact same type of problem!
The trick was to use
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
At the top of the Module, then modify your code to the following:
Dim HWNDSrc As Long
HWNDSrc = ie.HWND
SetForegroundWindow HWNDSrc
IE.document.all("regnumber")(1).Focus()
IE.document.all("regnumber")(1).Value = Reg + "." 'extra full stop will be removed on the next line
Application.SendKeys "{BS}"

Related

Why won't the ForeColor of this label visibly change when altered in a loop?

I am making a form that has groups of controls that I want to visibly enable/disable with a related toggle button (let's call them Group Toggles). Each group has a different variety of control types, so I made a common procedure to handle the toggling:
'constants for control ForeColors
Public Enum LabelForeColor
Default = 8355711
Off = 14277081
End Enum
Public Enum ListForeColor
Default = 4210752
Off = 12566463
End Enum
Public Sub EnableControl(Ctrl As Control, Enabled As Boolean)
With Ctrl
Select Case Ctrl.ControlType
Case acLabel
If Enabled Then .ForeColor = LabelForeColor.Default Else .ForeColor = LabelForeColor.Off
Debug.Print "LABEL", .ForeColor
Case acListBox
If Enabled Then .ForeColor = ListForeColor.Default Else .ForeColor = ListForeColor.Off
.Enabled = Enabled
Debug.Print "LIST", .ForeColor
Case acCommandButton
.Enabled = Enabled
Debug.Print "BUTTON", "NA"
Case acCheckBox
.Enabled = Enabled
Debug.Print "CHECK", "NA"
Case Else
Debug.Print "Control [" & .Name & "] is not of a type that EnableControl can handle."
End Select
End With
End Sub
Each group of controls is represented by a collection. When the form is loaded, every control with a particular tag property is added to the corresponding collection. The Group Toggles are not added to any collection and instead have event procedures that look like this:
Private Sub ToggleGroup1_AfterUpdate()
Dim State As Boolean
'a public function that converts the toggle button's value to a boolean
State = FormCommon.ToggleButtonState(ToggleGroup1.Value)
Dim iCtrl As Control
For Each iCtrl In Controls_ByPlant
FormCommon.EnableControl iCtrl, State
Next iCtrl
End Sub
When I click on a GroupToggle, all of the controls in the corresponding group visibly change appropriately, except for the labels. After an hour of troubleshooting, here's what I know:
The ForeColor property of the label does change, but not visibly.
When I call EnableControl on a label outside of a loop, the label visibly changes.
It doesn't matter if I pass the label object specifically to the subroutine or if I pass it from its group collection; the change is visible in both cases
If I toggle-disabled a label as part of the Group Toggle event and then call EnableControl specifically on that label to try to disable it again, there is no visible change (probably because the ForeColor property is already set to the "off" color)
Turning screen updating off with Application.Echo while the Group Toggle event runs and then turning it back on at the end of the event does not make a difference.
Making the Group Toggle event run with a For i = 1 to .Count instead of a For Each does not make a difference.
This problem also occurs when changing a different visual property instead, such as ForeTint.
(Per comments) Repaint does not make a difference
(Per comments) DoEvents does not make a difference
Why is this happening?
(First ever question, so apologies if I messed something up in the post)
This was interesting, but somewhat anticlimactic.
Your code does work for the labels, but what happens is this:
All labels are associated with input controls (as it is usual)
When you deactivate a group, you disable the input controls (.Enabled = Enabled)
This automatically sets the associated labels to a (system defined) light gray text color which cannot be changed.
This "disabled label" color is very similar to your LabelForeColor.Default color, so it is hard to see the change when toggling. But it does change.
Change your color constants to make the effect more visible:
Public Enum LabelForeColor
Default = vbRed ' 8355711
' the "Off" color is never visible, unless you add an un-associated label to a group
Off = vbBlue ' 14277081
End Enum
Edit: your test code FormCommon.EnableControl iCtrl, False works, because it only affects the label, but doesn't disable its associated list box.

Clicking an input checkbox in internet explorer

I'm trying to simply check "Yes" on an internet explorer input checkbox if the user clicks "Yes" to a MsgBox popup, or do nothing if the user clicks "No" on the MsgBox. No matter what I try, the code just doesn't seem to click the button I want it to click.
My code is as follows:
strPrompt = "Please look to see if this client has a valid brokerage account available for enrollment. Would you like to enroll this client in FRT service and submit a ticket?"
strTitle = "User Input Required"
iRet = MsgBox(strPrompt, vbYesNo, strTitle)
If iRet = vbYes Then
Enrollment_Button = objIE.document.getElementsById("classActionFlagReq")
Enrollment_Button.Value = "Y"
Else
MsgBox "No"
End If
I will also include the HTML source where the Yes and No buttons are contained:
<SPAN class=radio2>
<input name="classActionFlagReq" class="radio2" type="radio" value="Y"></input>
</span>
<SPAN class=radio2>
<input name="classActionFlagReq" class="radio2" type="radio" value="N"></input>
</span>
Where am I going wrong?
Updated Edited Code:
iRet = MsgBox(strPrompt, vbYesNo, strTitle)
If iRet = vbYes Then
Set Enrollment_Button = objIE.document.getElementById("classActionFlagReq")
Enrollment_Button.Checked = True
Else
MsgBox "No"
End If
You've got a few issues.
It's getElementById(), not getElementsById().
getElementById() returns an object. Use Set.
Set Enrollment_Button = objIE.document.getElementById("classActionFlagReq")
Use the Checked property to set a checkbox or radio button, not Value="Y".
Enrollment_Button.Checked = True
You're using the same name for both radio buttons, which is fine (that's how you define a radio "group") but if you want to access an individual radio button (so that you can select it), you need to assign a unique ID (or use a different selector method, like getElementsByName).
The properties name and id aren't interchangeable. You don't have any element with an id equal to "classActionFlagReq", so getElementsById is not finding any elements.
Note that id is unique, so you won't just be able to set both inputs to the same id like you did with the names.
Set ieRadio = objIE.document.all
ieRadio.Item("classActionFlagReq")(1).Checked = True
This solved my issue!

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.

Referencing unchecked checkboxes with vbscript causes an error

I am running into a problem with my vbscript code. My HTML code looks like this
<input type='checkbox' name='DisplayRow' id='DisplayRow1' />
<input type='checkbox' name='DisplayRow' id='DisplayRow2' />
<input type='checkbox' name='DisplayRow' id='DisplayRow3' />
This is done because above there is another checkbox that calls a javascript function that will check or uncheck all of the "DisplayRow" checkboxes. The javascript function uses getElementsByName to return all of the checkboxes named "DisplayRow".
When the form's submit button is clicked the action sends it to an ASP page (classic ASP) that grabs all of the objects on the calling form by using the Request.Form command. The Request.Form command looks at the "name" attribute, not the "id" attribute of the object.
This seems to be working fine for all of the other form objects. When it gets to the checkboxes because the "name" attribute uses the same name for all of the check boxes it returns an array. The individual checkboxes can be accessed like this:
Request.Form("DisplayRow")(x)
Where x references the individual checkbox in the array.
If the checkboxes are checked I can loop through the array without any problems. If 1 or more or all of the checkboxes are unchecked then when the code references the first checkbox in the array that is unchecked the page crashes. Nothing is executed after the Request.Form command fails.
I have tried enclosing the Request.Form("DisplayRow")(x) in an IsNull function in an If statement but it still takes the program down.
Has anyone else ran into this and found a work around?
Edit
For some reason stackoverflow is not letting me add more than one comment.
#Cory. Thanks for the information
#jwatts1980. Count works but it does not let me know which of the checkboxes are checked. If the count is greater than 0 I can loop through them but if the first one is unchecked I am right back where I started with a crashed page.
You cannot do it this way because unchecked checkboxes will not be submitted in the post, only the checked ones.
I would approach this differently:
First I would add a value attribute to the checkboxes, as so:
<input type='checkbox' name='DisplayRow' id='DisplayRow1' value="1" />
<input type='checkbox' name='DisplayRow' id='DisplayRow2' value="2" />
<input type='checkbox' name='DisplayRow' id='DisplayRow3' value="3" />
Notice the value is the same as the index, this will be needed in the code.
Next I would use .Split() to gather the checkboxes selected in an array, since the values will come in as a string separated by comma, ie: 1,2,3 to your .Form() value.
Rows = Request.Form("DisplayRow")
'check if any are selected first
If Rows <> "" Then
'make an array with each value selected
aRows = Split(Rows,",")
'loop through your array and do what you want
For i = lBound(aRows) to uBound(aRows)
Response.Write "DisplayRow" & aRows(i) & " was Selected."
Next
End If
This way you only process the results for the one's selected, and ignore the others.
I've always found this essential for my library...
'GetPostData
' Obtains the specified data item from the previous form get or post.
'Usage:
' thisData = GetPostData("itemName", "Alternative Value")
'Parameters:
' dataItem (string) - The data item name that is required.
' nullValue (variant) - What should be returned if there is no value.
'Description:
' This function will obtain the form data irrespective of type (i.e. whether it's a post or a get).
'Revision info:
' v0.1.2 - Inherent bug caused empty values not to be recognised.
' v0.1.1 - Converted the dataItem to a string just in case.
function GetPostData(dataItem, nullVal)
dim rV
'Check the form object to see if it contains any data...
if request.Form = "" then
if request.QueryString = "" then
rV = nullVal
else
if request.QueryString(CStr(dataItem))="" then
rV = CStr(nullVal)
else
rV = request.QueryString(CStr(dataItem))
end if
end if
else
if request.Form(CStr(dataItem)) = "" then
rV = CStr(nullVal)
else
rV = request.Form(CStr(dataItem))
end if
end if
'Return the value...
GetPostData = rV
end function
To use the function you simply pass in the form or query string item you're looking for and what to replace empty values with...
Dim x
x = GetPostData("chkFirstOne", false)
You can keep using your existing code by adding a single condition:
If Request.Form("DisplayRow").Count > 0 Then
'your code here
Else
'consider adding message saying nothing was selected
End If

Get recordset value to display checkbox as checked

I have a checkbox called "cbBatch" on one of my pages. When i check the box and submit the form, "cbBatch" is stored with a value of 1. On the next page, I need cbBatch to be "checked" if the value in the corresponding database field is "1". If it's 0, then unchecked. I have attempted to write it like this:
<input type="checkbox" name="cbBatch<%=vIndex%>" id="cbBatch<%=vIndex%>" value="<%If RS("batch") = 1 Then Response.Write " checked" End If%>">
my database field is an integer, fyi. and this isn't working. I did a response.write to verify that the value of the checkbox is indeed 1, and it is. so i don't understand why this isn't working. What am I missing?
Thanks!
Couple of things.
The recordset doesn't return integers, unless you explicitly cast the returned value to an integer.
Secondly, to set a checkbox to checked you don't set the value to checked. You just set the "checked" property.
This should work for you:
<input type="checkbox" name="cbBatch<%=vIndex%>" id="cbBatch<%=vIndex%>"<%If "" & RS("batch") = "1" Then Response.Write " checked" End If%>>