Get ActiveX checkbox status (checked or not checked) - vba

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.

Related

How can I get the checkbox.checked event to fire when I pass it as a parameter in vb.net?

I have programmed a long time but I’m relatively new to vb.net. And I’ve avoided subroutines and functions in which I passed parameters because I always get stuck. I’m trying to write a subroutine to pass information that will fill a TextBox or a checkbox with either the value from a table or clear the field or set to false. The first code below is an example of what I’ve been doing and this works. I trying to write a subroutine to pass 1.the name of the textbox or checkbox control on my form,2.the data row value, and 3.the column name in the table. The problem is when I passed a checkbox I can’t get the checked event to show on my control(CoreCol) that I passed. It knows it’s a checkbox and it will set the text of the checkbox too true or false but it won’t change the box checked.
This is an example of the old way that works. For a TextBox and a checkbox
' A Machine
If Not IsDBNull(r("A Machine")) Or Not IsNothing(r("A Machine")) Then
TbXMachA.Text = r("A Machine")
Else
TbXMachA.Text = ""
End If
If Not IsDBNull(r("A CO2 Box?")) Or Not IsNothing(r("A CO2 Box?")) Then
CkbxCO2BoxA.Checked = r("A CO2 Box?")
Else
CkbxCO2BoxA.Checked = False
End If
This works
LoadData2TextBox(Me. TbXMachA, r, "A Machine ")
This doesn’t
LoadData2TextBox(Me.CkbxCO2BoxA, r, "A CO2 Box?")
this is the sub routine I'm writing
Private Sub LoadData2TextBox(ByRef CoreCol As Control, CoreRow As DataRow, BoxStage As String)
If Not IsDBNull(CoreRow(BoxStage)) Then
If TypeOf CoreCol Is TextBox Then
CoreCol.Text = CoreRow(BoxStage)
End If
If TypeOf CoreCol Is CheckBox Then
CoreCol.??? = CoreRow(BoxStage)
End If
Else
CoreCol.Text = ""
End If
You know that CoreCol is a CheckBox so you can cast it as one then use it as a CheckBox.
If TypeOf CoreCol Is CheckBox Then
Dim myCheckBox = DirectCast(CoreCol, CheckBox)
myCheckBox.Checked = DirectCast(CoreRow(BoxStage), Boolean)
End If
Another cast in getting the boolean value out of CoreRow(BoxStage). The above code assumes this will work, but I am not sure what is in CoreRow(BoxStage). You may need to add some logic based on the value depending on what it is. For example:
myCheckBox.Checked = CoreRow(BoxStage) = "somevalue"

How do i hide a control in a report based on a field value

I have a checkbox control that i need to hide if a Boolean value is false. I have tried using the Detail_Format event to no avail. i put the field value into another checkbox, then tried setting the visible property from the value in the checkbox, no dice. In the image below, the rightmost checkbox is showing the value of the field that determines if the checkbox to the left of it should be shown. I have the following code
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.chkRequireverify = True Then
Me.chkVerified.Visible = True
Else
Me.chkVerified.Visible = False
End If
'Me.chkVerified.Visible = Me.chkRequireverify
End Sub
when i tried using the Detail_Paint event, it errored out telling me that i cant change the visible property in this event.
Your code looks to be correct and could be shortened to simply:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.chkVerified.Visible = Me.chkRequireverify
End Sub
However, I believe the event handler for the OnFormat event will only be evaluated when viewing the report in Print Preview view, rather than in Report view.
Whilst the OnPaint event fires in Report view, you cannot modify certain properties after the report has been formatted, the Visible property being one such property, along with the Height & Width properties (and so you also cannot shrink the checkboxes to zero size).
It's been a while since I've used Access and VBA, but I believe what you're missing is .Value after the checkbox name in the if clause.
Try this : If Me.chkRequireverify.Value = True

How to check for when a checkbox is checked in a Userform?

I have a Userform that has a Checkbox. I am able to check the value of it, but it is always False whether it is checked or not.
Update
This is how the UserForm is being called (This is in another UserForm ):
Private Sub AddOutgoingbtn_Click()
With New AddIncomingForm
.TopBottom.Value = False
.Show
.Repaint
End With
End Sub
End Update
I created a sub to look for a change in the value like:
Sub TopBottom2_Change()
With AddOutgoingForm
If .TopBottom2.Value = True Then TopBottom = True
If .TopBottom2.Value = False Then TopBottom = False
End With
End Sub
But no matter what I do the .TopBottom2.Value is always False.
I've put a breakpoint on the With line so that I know it is hitting this Sub, then I step through it each time. I open the UserForm and check the box, step through and the value is False, then I uncheck the box and step through. The value is still False.
I am not setting the value in any other way with VBA. I am checking the value in the UserForms code, not in any other place.
I have an If in a Calculation Module that is looking at this value for when it is true or falses, but it is always false.
Here are all the Properties of the Checkbox:
With AddOutgoingForm
That's referring to the form's default instance, which may or may not be the instance that's currently being displayed.
You have two options:
At the call site, instead of doing this (or something similar):
With New AddOutgoingForm
.Show
'...
End With
Do this:
AddOutgoingForm.Show
'...
That way you'll be working with the default instance and the checkbox value-check should work.
...but IMO that's a very very bad idea, because then your form contains code that will only ever work when you're showing the default instance.
Leave the call site alone, and NEVER refer to the default instance of a UserForm inside that form's code-behind. In other words change With AddOutgoingForm for With Me.
The Me keyword refers to the current instance - and that is what you want. Doing this will make the form work regardless of what the call site does.
Alternatively, just drop the With block altogether: With Me wouldn't be doing anything useful here.
I will assume the checkbox is indeed on your user form so just use me or you could use
AddOutgoingForm.TopBottom2.Value
But let me ask you, where is TopBottom and what is it a Boolean variable or another Checkbox? Also on the same form? The Subs are also all in the form? You have to be calling the form from somewhere so be careful between the worksheet, workbook, module variables. Is TopBottom a Global variable to the whole project (in a module called Global_Variables with Public in front of it perhaps?) You may not have access to TopBottom from inside of your form if you are not passing anything in or out.
Private Sub TopBottom2_Change()
If (Me.TopBottom2.Value) = True Then
Me.TopBottom.Value = True
Else
Me.TopBottom.Value = False
End If
End Sub
Cheers,
-WWC

set Checkbox value from Datareader

I am trying to pull information from a database into a form.
the database stores checkbox values as a single (-1 for true 0 for false)
However i am unable to set the .checked state of the checkbox with this.
frmTool.chkMeterFake.CheckState = dr("VALIDATE")
i have also tried
frmTool.chkMeterFake.CheckState = Convert.ToBoolean(dr("VALIDATE"))
Try using Checked property instead of CheckState:
frmTool.chkMeterFake.Checked = Convert.ToBoolean(dr("VALIDATE"))
The CheckState property is useful when you want your checkbox to have three possible states: checked, unchecked, or not set.
Why don't you use a bit column for booleans?
However, you can use this:
Dim validateIndex = dr.GetOrdinal("Validate")
frmTool.chkMeterFake.Checked = dr.GetFloat(validateIndex) = 0

Iterate on available control and check if this are enabled

I want iterate on GroupBox controls and check if the CheckBox are Checked or not. Actually I'm stuck on this:
For Each c In User.GroupBox3.Controls
If c.GetType.Name = "CheckBox" Then
If c.Checked = True ..?
End If
Next
How you can see I can't access to .Checked property, someone know how can I figure out?
Its about Types. CheckBox is a Type, which inherits from Control which is another Type. Since a ControlsCollection holds the items as Control, you have to cast to the specific Type in order to access the more specific properties and methods:
Long Form:
For Each c As Control In TabPage1.Controls
' check if it is the Type we are looking for
If TypeOf c Is CheckBox Then
' convert to desired type, do something
CType(c, CheckBox).Checked = True
End If
Next
CType converts/casts from Control to CheckBox.
Short Form:
For Each c As CheckBox In TabPage1.Controls.OfType(Of CheckBox)()
c.Checked = True
Next
This version filters to a given Type so the cast isnt needed.