Count number of checked boxes on a form

T

Todd

I would like to add a textbox (txtCheckedForms) that shows the number of
checkbox fields are checked on a form. Is there a simple way of doing this?
The only thing I've come up with so far is something like this...

If chkField1 = True then varCheckedForms = varCheckedForms + 1
if chkField2 = True then varCheckedForms = varCheckedForms + 1
....
ifchkField50 = True then varCheckedForms = varCheckedForms + 1
txtCheckedForms = varCheckedForms

The problem is, I have about 50 checkbox fields on my form. That would take
a long time and a lot of space! Also, the fields aren't actually named
chkFields1, 2, etc. They are all very unique. Is there an easier way?
Thanks!!!
 
F

fredg

I would like to add a textbox (txtCheckedForms) that shows the number of
checkbox fields are checked on a form. Is there a simple way of doing this?
The only thing I've come up with so far is something like this...

If chkField1 = True then varCheckedForms = varCheckedForms + 1
if chkField2 = True then varCheckedForms = varCheckedForms + 1
...
ifchkField50 = True then varCheckedForms = varCheckedForms + 1
txtCheckedForms = varCheckedForms

The problem is, I have about 50 checkbox fields on my form. That would take
a long time and a lot of space! Also, the fields aren't actually named
chkFields1, 2, etc. They are all very unique. Is there an easier way?
Thanks!!!

Off the top of my head, it would be easier to just add 1 to the text
control using each particular check box's AfterUpdate event:

If Me.CheckName = True then
Me.TextControlName = Nz(Me.TextControlName) + 1
Elseif Me.CheckName = False then
Me.TextControlName = Nz(Me.TextControlName) - 1
End If

Place the same code, changing the CheckName, in each check box's
AfterUpdate event.
This way you get a running total, and if a box is subsequently
unchecked, the sum is still correct.
 
K

Klatuu

Here is a function that will count how many check boxes on your form are
checked.

Private Function CountChecks() As Long
Dim lngX As Long
With Me
For lngx = 0 To .Controls.Count - 1
If .Controls(lngX).ControlType = acCheckBox Then
If .Controls(lngX).Value = True Then
CountChecks = CountChecks + 1
End If
End If
Next lngx
End With
End Sub
 
T

Todd

Great! Thanks!!!


Klatuu said:
Here is a function that will count how many check boxes on your form are
checked.

Private Function CountChecks() As Long
Dim lngX As Long
With Me
For lngx = 0 To .Controls.Count - 1
If .Controls(lngX).ControlType = acCheckBox Then
If .Controls(lngX).Value = True Then
CountChecks = CountChecks + 1
End If
End If
Next lngx
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top