469,336 Members | 5,299 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

How to Count Ticked Checkboxes

124 100+
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.
Aug 18 '10 #1

✓ answered by OldBirdman

True = -1 and False = 0, so @munkee's code lines 7-13 could be:
Expand|Select|Wrap|Line Numbers
  1. For Each ctl In Me 
  2.    If ctl.ControlType = acCheckBox Then intTotalChecked = intTotalChecked - ctl.Value 
  3. Next 
If you are going to use the count of checked checkboxes in your code, then it might be more useful as a function
Expand|Select|Wrap|Line Numbers
  1.  
  2. Function CountChecked() as Integer
  3. Dim intTotalChecked As Integer 
  4. Dim ctl As Control 
  5.  
  6. intTotalChecked = 0 
  7. For Each ctl In Me 
  8.    If ctl.ControlType = acCheckBox Then intTotalChecked = intTotalChecked - ctl.Value 
  9. Next 
  10.  
  11. CountChecked = intTotalChecked 
  12. End Function 
  13.  
Then the function could be used as:
Expand|Select|Wrap|Line Numbers
  1. If CountChecked <> 0 then MsgBox "No boxes are checked"
  2. 'or
  3. MsgBox CountChecked & " Boxes are checked"
or:
Expand|Select|Wrap|Line Numbers
  1. Me.txtTotalCount = CountChecked

4 8744
munkee
374 256MB
Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub countChecked()
  3. Dim intTotalChecked As Integer
  4. Dim ctl As Control
  5.  
  6. intTotalChecked = 0
  7. For Each ctl In Me
  8.    If ctl.ControlType = acCheckBox Then
  9.      If ctl.Value = True Then
  10.        intTotalChecked = intTotalChecked + 1
  11.      End If
  12.    End If
  13. Next
  14. MsgBox "Total Boxes Checked =" & intTotalChecked
  15. End Sub
  16.  
  17.  
Aug 18 '10 #2
OldBirdman
675 512MB
True = -1 and False = 0, so @munkee's code lines 7-13 could be:
Expand|Select|Wrap|Line Numbers
  1. For Each ctl In Me 
  2.    If ctl.ControlType = acCheckBox Then intTotalChecked = intTotalChecked - ctl.Value 
  3. Next 
If you are going to use the count of checked checkboxes in your code, then it might be more useful as a function
Expand|Select|Wrap|Line Numbers
  1.  
  2. Function CountChecked() as Integer
  3. Dim intTotalChecked As Integer 
  4. Dim ctl As Control 
  5.  
  6. intTotalChecked = 0 
  7. For Each ctl In Me 
  8.    If ctl.ControlType = acCheckBox Then intTotalChecked = intTotalChecked - ctl.Value 
  9. Next 
  10.  
  11. CountChecked = intTotalChecked 
  12. End Function 
  13.  
Then the function could be used as:
Expand|Select|Wrap|Line Numbers
  1. If CountChecked <> 0 then MsgBox "No boxes are checked"
  2. 'or
  3. MsgBox CountChecked & " Boxes are checked"
or:
Expand|Select|Wrap|Line Numbers
  1. Me.txtTotalCount = CountChecked
Aug 18 '10 #3
bullfrog83
124 100+
I like your code revision and the idea of a function! I've implemented both. Thanks birdman!
Aug 19 '10 #4
OldBirdman
675 512MB
Glad I could help. @munkee did the work, I just added a couple of suggestions.
Aug 19 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Pete | last post: by
4 posts views Thread by Karl | last post: by
5 posts views Thread by Paul Morrison | last post: by
2 posts views Thread by dad | last post: by
2 posts views Thread by Martin | last post: by
3 posts views Thread by Mr. SweatyFinger | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.