on_exit Event Procedure

T

tracktraining

Hi Everyone,

I have the following code to message the user to enter in an employee's name
if the user forgot to.

Private Sub txtEmpName_Exit(Cancel As Integer)
If IsNull(Me.txtEmpName) Then
MsgBox "Please Enter Employee Name"
Me.txtEmpName.SetFocus
End If
End Sub

This message appears when I click on the next txt box (leaving txtEmpName
Null). When the message appears and I click "OK", why didn't the cursor go
back to the txtEmpName txt box?

Please help if you can.

Thanks,
TrackTraining
 
N

Nicholas Scarpinato

Set the focus to another control on the form, then go back to txtEmpName,
like this:

Private Sub txtEmpName_Exit(Cancel As Integer)
If IsNull(Me.txtEmpName) Then
MsgBox "Please Enter Employee Name"
Me.SomeOtherField.SetFocus
Me.txtEmpName.SetFocus
End If
End Sub
 
T

tracktraining

THANKS!
--
Learning


Nicholas Scarpinato said:
Set the focus to another control on the form, then go back to txtEmpName,
like this:

Private Sub txtEmpName_Exit(Cancel As Integer)
If IsNull(Me.txtEmpName) Then
MsgBox "Please Enter Employee Name"
Me.SomeOtherField.SetFocus
Me.txtEmpName.SetFocus
End If
End Sub
 
L

Linq Adams via AccessMonster.com

You understand, of course, that this code ***will not*** ensure that the user
enters the employee name, don't you? If they never enter the txtEmpName
textbox, your code won't run, and the field will remain blank!

You only use validation code with a control's OnExit or AfterUpdate event if
you're checking data that has been entered against a ***certain format*** or
a ***certain value.*** To do validation of this type, checking ***whether a
field actually holds data*** the code needs to be in the form's BeforeUpdate
event. You also need to add a line to Cancel the update until the problem has
been corrected.

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtEmpName) Then
MsgBox "Please Enter Employee Name"
Cancel = True
Me.txtEmpName.SetFocus
End If
End Sub

This code will run ***anytime*** Access attempts to save the record, whether
you use a "save" button, move to another record, or close the form.
 

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

Top