Validating at least 1 ck box is checked

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a data input form with check boxes separated into sets of related
items (the sets of boxes are unrelated to each otherfor this task). One set
in particular has to have at least one box checked. It can have more than
one, up to all 6 checked. For validation, it does not matter which is
checked, just that at least one is checked. I'm thinking I can have code
count just those six and ensure the total is >1, or I can use if/then
statements, neither of which I'm particularly good doing (I'm relearning
code). With either path, if no box is checked, I need a dialog box to appear
notifying the user that they must select an option.

Does anyone have suggestions on which path or another approach, and help
with the validation code?
 
Add the value of each of the 6, then check the total in the form's before
update event.

The code should look something like (aircode):

Sub Form_BeforeUpdate(Cancel As Integer)
If ABS(NZ(Me.chk1,0) + NZ(Me.chk2,0) + _
NZ(Me.chk3,0) + NZ(Me.chk4,0) + NZ(Me.chk5,0) + _
NZ(Me.chk6,0)) = 0 Then
MsgBox "You must check at least 1", vbOKOnly, "Data Error:
Cancel = True
End If
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
You can just code:

IF Me!Checkbox1 = True OR _
Me!Checkbox2 = True OR _
Me!Checkbox3 = True Then
ELSE
Msgbox "All checkboxes empty, please select one",vbokonly,"Checkbox
problem"
END IF

Put this in the 'before update' event of the form

Dorian
 
On the before update event of the form, you can write the code

If Nz(Me.Chk1.False) = False And Nz(Me.Chk2.False) = False And
Nz(Me.Chk3.False) = False And Nz(Me.Chk4.False) = False And Nz(Me.Chk5.False)
= False Then
msgbox "Must enter one value
cancel = true ' wont let the user quit
End If
 
These all are good answers, I tried each and each worked. Thanks everyone.
 
I have created a form much like the one discussed in this question. Because
there were 20 or more checkboxes in each area, I found it easier to make
invisible textboxes totaling the values of the checkboxes in each area, (i.e.
AntecedentCheckBoxSum), and then basing the MsgBox on a sum of other than 0.

All of the messages popped up just fine, but then if the user clicked OK,
another message came up allowing them to either exit without saving or go
back to the form. I can't allow them to exit without saving.

This is the code, and you can see that "Cancel = True" is in there, but it
still lets them out without saving.

Private Sub Form_BeforeUpdate()

If AntecedentCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE ANTECEDENT MUST BE CHECKED."
Cancel = True
End If

If BehaviorCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE BEHAVIOR MUST BE CHECKED."
Cancel = True
End If

If ConsequenceCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE CONSEQUENCE MUST BE CHECKED."
Cancel = True
End If

If IntensityCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE INTENSITY MUST BE CHECKED."
Cancel = True
End If
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

Back
Top