473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
4
by: Pete | last post by:
Okay, I'm still stuck with this problem. Here's a quick recap/summary :- 1. Page 1:User checks 3 out of 10 checkboxes and submits form to page 2 2. Page 2:Item count shows 3 items. User checks...
4
by: Karl | last post by:
Hi all, I have a form with a junction table as the datasource. The form has three checkboxes. chkInactive - an Active / Inactive checkbox for the main record chkProj - an Active /...
5
by: Paul Morrison | last post by:
Hi, I have a set of 4 checkboxes and I want to have a button that will check all the boxes. I have looked on Google but can only find examples of it being in Javascript. Is there any way of...
2
by: dad | last post by:
I need help writing a query to display all boxes that are checked for a particular record. I have ten possible check boxes for each record and each record will have different boxes checked. Any...
2
by: Martin | last post by:
I am writing a custom validator by extending CustomValidator in order to validate three fields as follows: If checkbox true then textboxA and textboxB must have data in. The problem I am...
3
by: Mr. SweatyFinger | last post by:
I have a group of dynamically loaded check boxes. I need these clowns to check at least one. how can I do that?? thanks
4
by: haresh.amis | last post by:
hello to all, I m using .net 2.0 and i face a problem that is as under Well I have a checkboxlist which i bound in .cs page now I want to count that how many checkboxes ate checked ( In...
2
by: darkminos | last post by:
Hi, I have the following code int countCheckState = 0 for (int i = 0; i < CheckboxList.Count; i++) { if (CheckboxList.Checked) countCheckState++ if (countCheckState == 0)
1
greeni91
by: greeni91 | last post by:
Hi All, I am creating a database for one of my colleagues and he has asked me to make a query that has a drop down menu and takes into account that the checkboxes on the form have been ticked. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.