One check box at a time...

T

tvh

I have two check boxes in a form, but I would like the ability to have only
one checked at a time. What is the code for that? And what action would I
place the code under? (OnClick, maybe?)
 
R

Rick B

Sounds like an option group with radio buttons would be more appropriate.
This will allow you to select only one option from the group.

Rick B
 
W

Wayne Morgan

Rick is correct, placing the checkboxes in an Option Group frame will do
this automatically. Usually though, checkboxes can be one, none, or all.
Radio buttons are usually used for one or the other. Also, an Option Group
will return a single value for the group. The value will be 1, 2, etc,
depending on the number of objects in the group. The Option Group is then
the control that is bound to the table field, not the individual controls in
the Option Group.

To handle only allowing one checkbox at a time to be checked in code, use
the AfterUpdate event of each checkbox. Use code similar to the following.

Me.chkCheckbox1 = Not Me.chkCheckbox2

This will make the other checkbox value the opposite of the checkbox you
just clicked. In the example above, chkCheckbox2 would be the one you just
clicked. Reverse the 1 and 2 for the other checkbox. This will also force
the other checkbox to be checked if you uncheck a checkbox. If you want to
allow both to be unchecked, but not both checked, then try the following in
each AfterUpdate event:

If Me.chkCheckbox2 Then
Me.chkCheckbox1 = False
End If

Again, reverse the 1 and 2 for the other checkbox. This will force checkbox1
to be unchecked when you check checkbox2, but will do nothing if you uncheck
checkbox 2.

An Option Group will work like the first example.
 
T

tvh

That would no doubt be much better, but since I have the checkboxes already
in place, would you be able to tell me a code to use?

Thanks
 
T

tvh

Perfect! Thanks gentlemen!

Wayne Morgan said:
Rick is correct, placing the checkboxes in an Option Group frame will do
this automatically. Usually though, checkboxes can be one, none, or all.
Radio buttons are usually used for one or the other. Also, an Option Group
will return a single value for the group. The value will be 1, 2, etc,
depending on the number of objects in the group. The Option Group is then
the control that is bound to the table field, not the individual controls in
the Option Group.

To handle only allowing one checkbox at a time to be checked in code, use
the AfterUpdate event of each checkbox. Use code similar to the following.

Me.chkCheckbox1 = Not Me.chkCheckbox2

This will make the other checkbox value the opposite of the checkbox you
just clicked. In the example above, chkCheckbox2 would be the one you just
clicked. Reverse the 1 and 2 for the other checkbox. This will also force
the other checkbox to be checked if you uncheck a checkbox. If you want to
allow both to be unchecked, but not both checked, then try the following in
each AfterUpdate event:

If Me.chkCheckbox2 Then
Me.chkCheckbox1 = False
End If

Again, reverse the 1 and 2 for the other checkbox. This will force checkbox1
to be unchecked when you check checkbox2, but will do nothing if you uncheck
checkbox 2.

An Option Group will work like the first example.
 
Top