Cancel event for a text box

  • Thread starter Thread starter Ed
  • Start date Start date
E

Ed

The code in a subform does not cancel and does not give the focus to
TimeStartPeer. It does retain the old value and does show the message box.

Private Sub TimeStartPeer_AfterUpdate()
If DateDiff("n", [TimeStartPeer], [TimeEndPeer]) < 0 Then
DoCmd.CancelEvent
TimeStartPeer = TimeStartPeer.OldValue
TimeStartPeer.SetFocus
MsgBox "End Time can not be before Start Time!", _
vbCritical, "Meeting Time Error"
End If

End Sub
 
If you wish to cancel the event, use BeforeUpdate instead of AfterUpdate.
The focus will stay in the text box if you cancel that event.

A more significant issue, though, is that you are forcing the user to stay
in TimeStartPeer, when they may need to move ot TimeEndPeer to correct that
entry. Any comparison between fields is better done in the *form's*
BeforeUpdate event, when you know the user has the fields both filled in.

(The reason the SetFocus appears not to work is that the focus has not left
with the control's AfterUpdate runs, so the SetFocus actually does nothing,
and after the event is complete the focus moves on.)
 
try using the BeforeUpdate event instead, as

Private Sub TimeStartPeer_BeforeUpdate(Cancel As Integer)

If DateDiff("n", [TimeStartPeer], [TimeEndPeer]) < 0 Then
Cancel = True
MsgBox "End Time can not be before Start Time!", _
vbCritical, "Meeting Time Error"
End If

End Sub

hth
 
Back
Top