Set Focus Method

J

John

The following code is supposed to check if something was
entered in the Phone Number textbox. If the box was
left empty, the user is supposed to get an error message
(this part works) when they attempt to go to the next
field and, after they click OK on the message box, the
focus should be set back on the Phone Number textbox.
For some reason the focus does not go back onto the Phone
Number textbox. I do not want the user to be able to leave
the text box till they enter in a value, is the setfocus
method the right method or should I be using something
else.

Thanks,
John

Private Sub txtPhoneNum_LostFocus()
txtPhoneNum.SetFocus
If Len(txtPhoneNum.Text) = 0 Then
MsgBox "You must enter the phone number!",
vbExclamation, "Insufficient Data"
txtPhoneNumber.SetFocus
End If
End Sub
 
F

fredg

John said:
The following code is supposed to check if something was
entered in the Phone Number textbox. If the box was
left empty, the user is supposed to get an error message
(this part works) when they attempt to go to the next
field and, after they click OK on the message box, the
focus should be set back on the Phone Number textbox.
For some reason the focus does not go back onto the Phone
Number textbox. I do not want the user to be able to leave
the text box till they enter in a value, is the setfocus
method the right method or should I be using something
else.

Thanks,
John

Private Sub txtPhoneNum_LostFocus()
txtPhoneNum.SetFocus
If Len(txtPhoneNum.Text) = 0 Then
MsgBox "You must enter the phone number!",
vbExclamation, "Insufficient Data"
txtPhoneNumber.SetFocus
End If
End Sub
John,
Use the txtPhoneNum BeforeUpdate event.
And it's the Value property, not the Text property you want to check.

If IsNull(txtPhoneNum) Then
MsgBox "You must enter the phone number!",
vbExclamation, "Insufficient Data"
Cancel = true
End If

The Cancel = True will return the focus to the txtPhoneField.
 

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