Multiple ComboBoxes but one must be Checked

  • Thread starter Thread starter Flydianslip
  • Start date Start date
F

Flydianslip

I have 22 CheckBoxes and I want to make sure one of them is required to be
Checked. If not I want a MsgBox to come up.

I'm testing it now, on just one CheckBox but I can't even get this to work.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Check216 = "No" Then
MsgBox "Please fill in the Job Type"
Cancel = True
End If
End Sub

Any help would be appreciated. Thank you.
 
Flydianslip said:
I have 22 CheckBoxes and I want to make sure one of them is required to be
Checked. If not I want a MsgBox to come up.

I'm testing it now, on just one CheckBox but I can't even get this to
work.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Check216 = "No" Then
MsgBox "Please fill in the Job Type"
Cancel = True
End If
End Sub

Any help would be appreciated. Thank you.

The value of a checkbox is of boolean data type (True or False), so your
code should be:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Check216 = False Then
MsgBox "Please fill in the Job Type"
Cancel = True
End If
End Sub

(Get into the habit of using indentation - you'll thank yourself further
down the road)
 
I have 22 CheckBoxes and I want to make sure one of them is required to be
Checked. If not I want a MsgBox to come up.

If these are bound to 22 fields in a table... your table design is almost
certainly WRONG. Could you consider instead a *single* numeric field with
values 1 to 22 (or maybe to 25 when you someday add three new options)?

Then you could use an Option Group control with 22 radio buttons or
checkboxes; the Option Group control would have just one value and allow only
one box to be checked.
I'm testing it now, on just one CheckBox but I can't even get this to work.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Check216 = "No" Then

This is failing because you're comparing the checkbox control (which can take
on values 0 for False, -1 for True) with a text string "No". This comparison
will never be true. You could replace "No" with a zero (no quotes), but you'ld
need 22 ugly If blocks... you should really reconsider the table design!
 
***IF*** you have 22 checkboxes and one or more must be checked, you can use

IF Me.Check216 Or Me.Check217 or Me.CheckAA Or Me.CheckN Or ... THEN
'One or more is checked so do nothing
ELSE
MsgBox "Please check something"
Cancel = True

END IF

This sounds as if you have a mis-designed data structure. See some of the
other posts in this thread.

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
Back
Top