CheckBox BeforeUpdate feature/bug?

  • Thread starter Thread starter Dan Goldman
  • Start date Start date
D

Dan Goldman

I'm having trouble validating a checkbox in its BeforeUpdate event - it's
behaving quite differently from a textbox's BeforeUpdate:
In a textbox's BeforeUpdate event (which gets fired only when the user
tries to leavethe control), if I determine that the new data is invalid and
set Cancel=True, the textbox keeps the invalid data, so until the user
explicitly undoes it he won't be able to leave the control.
But in a checkbox (where the BeforeUpdate gets fired as soon as the user
changes its value), setting Cancel=True causes Access to undo the change,
and when the user tries to leave the control, it gets fired again, but this
time with the chekbox's original value.
This behavior is unreasonable - if Access wants to undo the change, it
shoudn't fire the BeforeUpdate again; if it wants to continue firing the
event it shouldn't undo the change.
To demonstrate this behavior, create a new form, add a checkbox named
"Check0" with its DefaultValue set to False, and add a textbox so we'll have
where to tab to, then add this code:

Private Sub Check0_BeforeUpdate(cancel As Integer)
Dim prompt
If Check0 Then
prompt = "are you sure you want to check the checkbox?"
Else
prompt = "are you sure you want to clear the checkbox?"
End If
If Not MsgBox(prompt, vbYesNo + vbQuestion) = vbYes Then cancel = True
End Sub

If you check the checkbox, you get the prompt "are you sure you want to
check the checkbox?".
If you answer "No", and then try to set focus to the textbox, you get the
prompt "are you sure you want to clear the checkbox?" which is definitely
out of place.

This has been tested in both Access 97 and 2003.
I couldn't find anything on this in the newsgroups. Can anyone explain
this behavior, and what's the best workaround (validate the checkbox in the
AfterUpdate event?)

Dan.
 
Yes, Dan, the check box (toggle button, and option button when stand alone)
behave as you describe. It kinda makes sense since changing one of these
does alter the Value.

In general, I have found it better to use the AfterUpdate event of the
control to run any code-level validation, and just undo the control if you
don't like the change.
 
Thanks, I'll use your recommendation.
I agree that it makes sense to fire the BeforeUpdate when the value gets
toggled, before the control loses focus. But I can't understand why it
should fire again after the vaue gets undone (although it's not such a big
deal - I can live with it).
Dan.
 
Back
Top