cannot set focus

  • Thread starter Thread starter Song
  • Start date Start date
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
 
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.
 
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
 

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