Dynamic radio VBA - vba

On this internal website it allow me enter a data and depending on the data the radio box will be enable. How do I know when the radio box is enabled.
Enabled
input id="Answer1" name="Answer" type="radio" value="C"
Disabled
input id="Answer1" name="Answer" disabled="disabled" type="radio" value="C"
When it is disabled the getAttribute will return "disabled" but when it is enabled it will return run time error
Invalid use of Null (Error 94)
if I used
msgbox ie.document.all("Answer1").getAttribute("disabled")
I tried the following if statement and none will caught it when it is enabled
ie.document.all("Answer1").getAttribute("disabled") = ""
ie.document.all("Answer1").getAttribute("disabled") = Null
ie.document.all("Answer1").getAttribute("disabled") <> "disabled"
ie.document.all("Answer1").getAttribute("disabled") = "disabled"

Reference:HTML disabled Attribute
Disabled is a boolean attribute. This means that is value should not be set (e.g. disabled="disabled"). If disabled is present then the option is disabled.
<option value="volvo" disabled>Volvo</option>
You should be able to use msgbox ie.document.all("Answer1").disabled. If this doesn't work trap the error
Function isDisabled(ele As Object) As Boolean
On Error Resume Next
isDisabled = ele.getAttribute("disabled")
On Error GoTo 0
End Function

I also find this worked too in if statement
if isnull(ie.document.all("Answer1").disabled) then
msgbox "Radio box disabled"
else
msgbox "Radio box enabled"
end if

Related

How to click a "button" if the function GetElementById returns Nothing

I'm doing a simply program to collect some e-mail's information from a "Disposable E-mail" (www.yopmail.com) page, when i try to press the "button" to open the e-mail using the functions GetElementById and InvokeMember, it doesn't works, the program says that GetElementById returns Nothing and the program stops.
I've tried in another pages using GetElementById and InvokeMember("click") and it works but, i am not sure why doesn't work in this case
When i inspect the element, it shows the next:
<div class="m" onclick="g(2,0);" id="m2"><div class="um"><a class="lm" href="m.php?b=hello&id=me_ZGxkZQNkZGt1ZQH4ZQNkZQN5ZGN5ZN=="><span class="lmfd"><span class="lmh">13:50</span><span class="lmf">[SPAM]PINTRILL</span></span><span class="lms">PT | Snoopy & Friends are Back.</span></a></div></div>
wb.ScriptErrorsSuppressed = True
wb.Navigate("www.yopmail.com?hello")
wait(5)
While fullyLoaded = False
If wb.Url.Host <> "www.yopmail.com?hello" Then
MsgBox("Yopmail 'finished' loading")
wait(2)
fullyLoaded = True
Else
MsgBox("inside while")
wait(5)
End If
End While
wb.Document.GetElementById("m2").InvokeMember("click")
When the program stops, it shows me this:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.HtmlDocument.GetElementById(...) gave back Nothing.

Problem inserting value into "Input" field

I run into a weird problem when trying to automate inserting data into a website. Specifically one input field, here is html piece of that website:
<input class="maskAcctNbr" id="masked_consumer" type="text" size="20" maxlength="20" value="" autocomplete="off">
When I will insert value into that field using this code and click on "Lookup" button:
Dim SE As MSHTML.IHTMLElement
Set SE = HTMLDoc.getElementById("masked_consumer")
SE.Value = "574844"
The page is telling me that I'm missing information.
After I will manually click on that field and enter that number, the underlying html code changes to:
<input class="maskAcctNbr" id="masked_consumer" type="text" size="20" maxlength="20" value="" autocomplete="off" real="574844">
New real property shows up.
Any ideas how I can solve that using VBA? I tried, click and focus, and nothing really works, until I use mouse to click on that field and manually enter that number.
Thank you!
Try to refer example below may help you to solve the issue.
Example:
Sub demo()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate "http://example.com"
IE.Visible = True
While IE.Busy
DoEvents
Wend
Set TrackID = IE.document.getElementById("masked_consumer")
TrackID.Focus
Application.SendKeys ("(574844)"), True
Application.SendKeys ("{ENTER}"), True
End Sub
I ran into a similar issue one time, and activating .OnClick event of that field helped.
In your case it would be
SE.onclick
before setting the value.
There can also be some other events such as OnChange that may result in "no data entered" errors, when they are not triggered.
Good luck :)

Changing HTML elements with Webbrowser

I have a question, how to change HTML input value?
<select name="sys_lenght" aria-controls="systable" class>
<option value="20">Value 1</option>
<option value="40">Value 2</option>
<option value="80">Value 3</option>
</select>
I used this code
For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
If Elementz.Name = "sys_lenght" Then
Elementz.setAttribute("value", "80")
End If
Next
But it doesn't change the input value, only the text "Value 3".
How can I solve this problem? Thanks
sys_lenght's value indicates which option with the specified value is selected. Thus if you set sys_lenght.value to "80" it will select Value 3.
To change the value of the currently selected option you have to get a reference to that first. You can do so by getting the selectedIndex of sys_lenght, and then get the specific item from that index.
For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
If Elementz.Name = "sys_lenght" Then
'Get the index of the selected option.
Dim SelectedIndex As Integer = Integer.Parse(Elementz.GetAttribute("selectedIndex"))
If SelectedIndex >= 0 Then
'Get the option element from the resulting index.
Dim SelectedOption As HtmlElement = Elementz.Children(SelectedIndex)
SelectedOption.setAttribute("value", "80")
Else 'No option selected.
MessageBox.Show("No option selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Exit For 'We found what we were looking for; stop looping.
End If
Next
First you have to understand how html open and close tag, you forgot to close the html selection option open tag i mean the last greter sign > then use javascript query to find the value within the option example
WebControl1.ExecuteJavascript('document.querySelector('option [value='your value']').selected=true;")
hope its help?

How to reference the users current textbox?

In access I am trying to not let the user exit a textbox unless they have filled it out (i.e. not left it null). This is to be done in a function in order to cut down on code. In VBA is there some way to stop the user from exiting the function? (I know about setting the focus but I need the code to work from a multitude of different textboxes)
For reference my current code is as follows;
Function Reload()
If IsNull(EmployeeID.Value) Or IsNull([First Name].Value) Or IsNull([Surname].Value) Or IsNull(DOB.Value) Or IsNull(Position.Value) Or IsNull(Position.Value) Or IsNull(Mobile.Value) Or IsNull(Email.Value) Or IsNull(Address.Value) Or IsNull(Suburb.Value) Or IsNull(Postcode.Value) Or IsNull([Start Date].Value) Or IsNull(UserLogin.Value) Or IsNull(UserPassword.Value) Then
MsgBox "Please fill out all fields"
Else
DoCmd.RunCommand acCmdSaveRecord
Form.Refresh
End If
End Function
Thanks
The best technique I know is to use the BeforeUpdate event to run data validation. Using your if statements if the data is not valid then set
Cancel = True
and the data will not write. You would probably prefer to disable the default record navigation and form close buttons and use custom buttons so that you can trap the invalid data error and prevent moving to a new record or closing the form. But even if you allow the user to use the built in navigation buttons or close button Access will throw up error messages about being unable to save the current record and prompt the user if he wants to proceed.
You can also go a step further and captue which fields are invalid and present a message box.
Msgbox ("The following fields were left blank and must be entered to save this record:" & vbCrLf & "Field 1" & vbCrLf & "Field 2")
A little modification of the text input limiter i use in our chat room should help. New function I have called it MustINPUT and call it on the LostFocus of the text boxes that you don't want to move from with out input.
Private Sub MyTextBox_LostFocus()
Call MustINPUT (Me.MyTextBox, 0)
End Sub
Just change the message for your users.
Sub MustINPUT(ctl As Control, iMaxLen As Integer)
'Call MustINPUT(Me.txtSayThis, 0)
If Len(ctl.Text) = iMaxLen Then
MsgBox "Please Type something", vbExclamation, "Input Needed"
ctl.Text = Left(ctl.Text, iMaxLen)
ctl.SelStart = iMaxLen
End If
End Sub
Hope this help. DR ,

Get ActiveX checkbox status (checked or not checked)

How do you access the value of a checkbox' status. I was expecting to see something like a Value property which would be True if the checkbox is checked. However, the following does not work:
Worksheets("Summary").Shapes("chkbxRunLocally").controlform.Value
It throws:
Object does not support this property or method
If this is not where the property stored, where then?
I guess you're not using an ActiveX checkbox, this one has a .value but if you access it directly as an object variable, i.e.
Debug.Print Worksheets("Summary").chkbxRunLocally.Value
If you are using a normal Excel shape (non ActiveX), Try this:
Debug.Print Worksheets("Summary").Shapes("chkbxRunLocally").ControlFormat.Value = 1
or also
Debug.Print Worksheets("Summary").CheckBoxes("chkbxRunLocally").Value = 1
Notice the =1 which is True if the shape is checked, False if unchecked. When it is unchecked, the returned .Value is -4146 which is also true if converted directly to a Boolean. The test =1 decides (as Boolean) whether the control is actually checked or not.