Re setting focus to a control

M

Marc

Hi,

I have a data entry form in Access 2000. If the operator tries to tab out of
certain fields without entering data, I want to prompt them to do so, then
return the cursor to that field. I tried:
Private Sub Salesperson_LostFocus()
If IsNull(Me.Salesperson.Value) Then
MsgBox ("Please Select a Salesperson")
DoCmd.GoToControl "Salesperson"
Exit Sub
End If

End Sub

This gives me the Message Box but the cursor goes to the next field. I've
also tried:
Salesperson.SetFocus and tried moving the DoCmd above the MsgBox, but
neither worked.

I'm not very experienced in VB. What am I doing wrong?

Thanks,
 
M

Marc

Hi Sprinks,

Thanks for the suggestion. Sorry to be thick about this, but where do I put
that? I tried the line after "If IsNull..."
after the MsgBox, after "Exit Sub" and after "End If" and nothing happened.
Can you help more?

Thanks,

Marc
 
B

Brendan Reynolds

You need to use the Exit event procedure rather than the LostFocus event
procedure. Cancel is a built-in argument of the Exit event procedure.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
M

Marc

That worked!

Thank you both,

Marc

Brendan Reynolds said:
You need to use the Exit event procedure rather than the LostFocus event
procedure. Cancel is a built-in argument of the Exit event procedure.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Top