Set Focus Method

  • Thread starter Thread starter John
  • Start date Start date
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
 
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.
 
Back
Top