DoCmd.GoToControl question

  • Thread starter Thread starter dvlander
  • Start date Start date
D

dvlander

I wrote the brief VBA code below in the After Update event of a text box
control. It's working fine except the focus is ending up in the next field
in the Tab Order instead of returning to the this text box so the user can
enter data that fits the If statement's criteria. Is there something I need
to do differently? Thanks in advance for the help.

Dale

Private Sub txtAssignmentStartDate_AfterUpdate()
If CDate(txtAssignmentStartDate) < CDate(txtProjectStartDate) Then
MsgBox "The Assignment Start Date cannot be earlier than the Project Start
Date of " & txtProjectStartDate
DoCmd.GoToControl "txtAssignmentStartDate"
Else
End If
End Sub
 
I wrote the brief VBA code below in the After Update event of a text box
control. It's working fine except the focus is ending up in the next field
in the Tab Order instead of returning to the this text box so the user can
enter data that fits the If statement's criteria. Is there something I need
to do differently? Thanks in advance for the help.

Dale

Private Sub txtAssignmentStartDate_AfterUpdate()
If CDate(txtAssignmentStartDate) < CDate(txtProjectStartDate) Then
MsgBox "The Assignment Start Date cannot be earlier than the Project Start
Date of " & txtProjectStartDate
DoCmd.GoToControl "txtAssignmentStartDate"
Else
End If
End Sub

Wrong event.
Use the control's BeforeUpdate event.
It has a Cancel argument. Just set Cancel = True and you'll go back
to the control for re-entry.
Also, no need for the Else if you do not have an Else.

Private Sub txtAssignmentStartDate_BeforeUpdate(Cancel as Integer)
If CDate(txtAssignmentStartDate) < CDate(txtProjectStartDate) Then
MsgBox "The Assignment Start Date cannot be earlier than the Project
Start Date of " & txtProjectStartDate
Cancel = True
End If
End Sub
 
That is one technique, but you can also use the AfterUpdate event. Actually,
I like to put my validation code in the forms BeforeUpdate event.

I've never used docmd.gotocontrol, so I don't know why that isn't working.
I've always used:

me.txtAssignmentStartDate.SetFocus

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
Back
Top