Clicking an input checkbox in internet explorer - html

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!

Related

Use checkbox to filter data

I want when the checkbox or button (both at the top of this screen) is checked, it does not show the items that are "Canceled" or "Received" in the Status column.
Items names:
status search box is called "S_Status"
status dropdown menu is called "Status"
the dropdown option is pulled from Query "N_Q_PlacingOrders_Status" which pulls from Table "N_T_Placing Orders_Status".
Checkbox name is "CheckBox"
I tried the code below but I know I am missing a couple things that I have not been able to find in other blogs/forums.
Private Sub CheckBox_Click()
If CheckBox = True Then
DoCmd.ApplyFilter , Status = True
If CheckBox = False Then
Exit Sub
End If
End If
End Sub
Looks like you were on the right track. Two things of note:
1) In the following code, these lines (If CheckBox = False Then |Exit Sub |End If) are basically pointless, unless you want the False to actually do something, its just extra fluff. Therefore, removing the secondary If statement is beneficial.
If CheckBox = True Then
DoCmd.ApplyFilter , Status = True
End If
2) Using filters in this situation may not be the easiest way to accomplish your goal. Try this:
If CheckBox = True Then
Status.RowSource = "SELECT * FROM N_Q_PlacingOrders_Status WHERE " & _
"Status NOT IN ('Recieved', 'Cancelled')"
End If
NOTE: It may need a modification to the SELECT statement, as required by the rows you are trying to pull (but I don't have that information).

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.

Button click unclick ms-access

In access form, I'm hoping to click on a button and add information to an existing record using an update query. Ideally, when this happens, the button will change colors and appear 'activated'.
Then, if the user decides that the information that has been added needs to be removed, they can click the same button again. This removes the previously added information from the table and changes the button appearance to 'inactive'
You can't change the colour of a command button if you are using Accesss 2003 or earlier, but you can simulate a button using a label, and obviously you can change a labels caption and colours.
If you are using 2007 on-wards then substitute the Label name to your Command button name.
Using the On_current property of your form use something like
If Me.AddData = 1 Then
Me.YourLabel.Caption = "Remove Data"
Me.YourLabel.BackColor = VbRed
Else
Me.YourLabel.Caption = "Add Data"
Me.YourLabel.BackColor = VbGreen
End If
Then use a similar logic to run your update code from the On_click property of the label, based on the value of AddData.
If you're willing to have -1 and 0 in AddDate these can be easily converted to TRUE/FALSE.
This is taking a few liberties with the info you've given, but it's getting late in the day.
Your form has a command button (Command7) and a textbox (Text8).
Text8 has a control source linked to AddData (so it shows -1 or 0).
This code executes whenever you move to a different record. It checks the value in Text8 and changes the colour of the command button accordingly:
Private Sub Form_Current()
With Me
Select Case .Text8
Case -1
.Command7.BackColor = RGB(0, 255, 0)
Case Else
.Command7.BackColor = RGB(0, 0, 255)
End Select
End With
End Sub
This code on the click event of the command button will change the value in Text8 from True to False and vice versa.
It will then requery the form, forcing the Form_Current event to fire.
A requery moves the recordset back to the first record, so the bookmark moves it back to the record you were looking at.
Private Sub Command7_Click()
Dim bkmrk As String
bkmrk = Me.Bookmark
With Me
.Text8 = Not CBool(.Text8)
.Requery
Me.Bookmark = bkmrk
End With
End Sub
Edit:
Scrap that -1 and 0 malarkey....
Change the Select Case to Case 1 in the Form_Current event.
Change the .Text8 = Not CBool(.Text8) in the Command7_Click event to
.Text8 = Abs(Not CBool(.Text8 * (-1)))

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%>>