Checkbox on form

  • Thread starter Thread starter Froto
  • Start date Start date
F

Froto

I received this code from Fredg

Code the Check Box AfterUpdate event:
If Me![CheckBoxName] = -1 Then
[ControlA].SetFocus
End If

Code the [ControlA] Exit Event:
If IsNull(Me![ControlA]) Then
MsgBox "Yopu must enter data in this control."
Cancel = True
Else
[ControlB].SetFocus
End If

Code The ControlB exit event:
If IsNull(Me![ControlB]) Then
MsgBox "You must enter data in this control."
Cancel = True
End If

This code works great, but the problem is when the user
checks the checkbox accidentaly, it doesn't allow him to
uncheck the box, but keeps popping up with the msgbox,
how would I keep the code the way it is because it works
great once checked, but just to give the user the ability
to uncheck the checkbox. Thanks for all the help
 
Froto,

The If...Then construct provides for an Else block as well. So where Fred
suggested:
If Me![CheckBoxName] = -1 Then
[ControlA].SetFocus
End If

....you could add an Else clause to cater for situations where the criteria
is not met:
If Me![CheckBoxName] = -1 Then
[ControlA].SetFocus
Else
[ControlB].SetFocus 'or you can do something else
End If

You can also provide more test criteria if the first one isn't met:
If Me![CheckBoxName] = -1 Then
[ControlA].SetFocus
ElseIf X = Y Then
'Do whatever you want
End If

You can nest If...Then clauses:
If Me![CheckBoxName] = -1 Then
[ControlA].SetFocus
ElseIf X = Y Then
If A = B Then
'Do this
Else
'Do that
End If
End If

....or you can have multiple inline conditions:
If Me![CheckBoxName] = -1 AND B = C Then
[ControlA].SetFocus
ElseIf X = Y Then
If A = B AND M < 123 Then
'Do this
Else
'Do that
End If
End If

I hope this clarifies things a bit.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Back
Top