Check filed for input, if none return cursor

G

Guest

Someone please help me. I have 4 fields that I want to check as the user
leaves the field if they have filled them in. Example form comes up, cursor
on txtField1, user tabs (or else) off of txtField1, msgbox to alert user must
enter something, put cursor back in txtField1 box.

I have tried using the Private Sub txtField1_LostFocus() event to check the
field. However to check you have to setfocus (txtField1.setfocus) then check
if txtField1.text = "", and if so give message, and return cursor.

It looks like it iterrates many times through this, and I don't understand
why (perhaps once for each record ... or perhaps because each time I setfocus
it is loosing focus and tripping the event? Also the txtField1.setfocus
doesn't set the cursor back in the field, it remans in the next field.
Please help me with some code so that I can understand the behavior, and
principals.
Thank you in advance.
 
G

Guest

Why not in the after update event of txtField1:

If me![txtField1] = "" or isnull(me![txtField1]) then
Mgsbox "your message"
Me![txtField1].setfocus
end if
 
G

Guest

Examine the Value property of the text box, not its Text property. You don't
need to move focus back to the control to do this. Also test for Null not a
zero-length string, assuming its default value is not a zero-length string:

If IsNull(Me.txtField1) Then
MsgBox "Field 1 must be completed.", vbExclamation, "Invalid Operation"
End If

You won't be able to move focus back to the control in its own LostFocus
event procedure, however. To do that you'd need to put the code in the next
control's GotFocus event procedure.

Do this at control level, however, assumes that the user tabs through the
controls in sequence. If they skip over controls by selecting another one
with the mouse the event won't occur. You should cover this by also testing
for each control being Null in the form's BeforeUpdate procedure, and set the
return value of the event procedure's Cancel argument to True if any of the
controls are Null.

Ken Sheridan
Stafford, England
 

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