This may be a rather unusual question but how do you count how many checkboxes have been ticked? There's some logic that I want to apply to a form that has 3 unbound checkboxes but in order to apply it I need to be able to count how many have been ticked.
How to Count Ticked Checkboxes
Collapse
X
-
Tags: None
-
Code:Sub countChecked() Dim intTotalChecked As Integer Dim ctl As Control intTotalChecked = 0 For Each ctl In Me If ctl.ControlType = acCheckBox Then If ctl.Value = True Then intTotalChecked = intTotalChecked + 1 End If End If Next MsgBox "Total Boxes Checked =" & intTotalChecked End Sub -
True = -1 and False = 0, so @munkee's code lines 7-13 could be:
If you are going to use the count of checked checkboxes in your code, then it might be more useful as a functionCode:For Each ctl In Me If ctl.ControlType = acCheckBox Then intTotalChecked = intTotalChecked - ctl.Value Next
Then the function could be used as:Code:Function CountChecked() as Integer Dim intTotalChecked As Integer Dim ctl As Control intTotalChecked = 0 For Each ctl In Me If ctl.ControlType = acCheckBox Then intTotalChecked = intTotalChecked - ctl.Value Next CountChecked = intTotalChecked End Function
or:Code:If CountChecked <> 0 then MsgBox "No boxes are checked" 'or MsgBox CountChecked & " Boxes are checked"
Code:Me.txtTotalCount = CountChecked
Comment
-
Comment
-
Comment