Retrieving values of checkboxes

  • Thread starter Thread starter DaveF
  • Start date Start date
D

DaveF

I want to loop through a group of 3 checkboxes on a form and, if checked,
retrieve their value.

Something like this:

For i = 1 To 3
If .CheckBoxes(i).Checked = True Then
MsgBox .CheckBoxes(i).value
End if
Next

But I am not sure how to properly reference the checkbox control. What
parent object do I use?

The Access Help example uses the Assistant.NewBalloon parent object. I need
to use a Form as the parent object but I can't get the proper syntax to
reference the checkboxes object
 
If the checkboxes are not part of an option group, you
can just refer to them using their field names just like
any other field. For instance Me!MyCheckBox would refer
to the check box named MyCheckBox on the current form (or
Me![My Checkbox] if the name contains a space).

I haven't had the need to use option groups before, so
I'm not sure of the exact syntax for referring to items
within them. I would expect that it would either be Me!
MyOptionGroup!MyCheckbox or it would be the same as above.

As far as looping through them, if you know that it is
three particular fields that you want to check I would
just list three independent statements and skip the loop.

Usually you would loop through items that are part of a
collection. The form has a "Controls" collection, which
the checkboxes are a part of, but so are all of the other
controls. You would have to loop through all of the
controls and check to see if the type was a checkbox.

An option group is also a collection within the controls
collection, so if the items were part of an option group
you could loop through them. But for only three items it
seems that it would be easier to just check each one to
see if it was checked (= True) and if so take whatever
action you want to take.

Hope this helps.
 
There is something else to consider with checkboxes. If
they are checked, their value is true or -1. If
unchecked, their value is false or 0.

If you are looking to retrieve the value of the label that
is attached to the checkbox, you have to use a different
approach. For instance, say you have a checkbox labelled
Programmer (you'd check this if the person was a
programmer). By checking this checkbox, the value of the
checkbox will simply be true or -1, and does not in any
way refer to the label, which is a separate control on
your form.

You might consider including hardcoded responses in your
code.
If chkMyCheckbox = true then
msgbox "Programmer"
End If
 
Back
Top