cannot set focus

S

Song

My following code cannot set focus to DateTo. It jumped to next control.
Where did I do wrong?

Private Sub DateTo_Exit(Cancel As Integer)

If Me.DateFrom > Me.DateTo Then
MsgBox "DATE FROM cannot be later than DATE TO", vbInformation
DateTo.SetFocus
End If
End Sub
 
M

Marshall Barton

Song said:
My following code cannot set focus to DateTo. It jumped to next control.
Where did I do wrong?

Private Sub DateTo_Exit(Cancel As Integer)

If Me.DateFrom > Me.DateTo Then
MsgBox "DATE FROM cannot be later than DATE TO", vbInformation
DateTo.SetFocus
End If
End Sub


Try using the BeforeUpdate event instead of the Exit event.
Then, you can use Cancel = True in place of the SetFocus.
 
K

Ken Snell \(MVP\)

Or add one step to the Exit event procedure to cancel the event:

Private Sub DateTo_Exit(Cancel As Integer)

If Me.DateFrom > Me.DateTo Then
MsgBox "DATE FROM cannot be later than DATE TO", vbInformation
Cancel = True
End If
End Sub
 
Top