Hi,
I have user form that has a textbox on it (.txtPhone), I want to validate
that the data is all numbers and if not, display message box "Text Only" and
then have the focus stay in the text box.
I currently can validate if .txtPhone ISNumeric (see code below), but I can
NOT get the focus back to the .txtPhone textbox. Any advice, assistance,
guidance, would be greatly appreciated.
Private Sub txtPhone_Exit(ByVal cancel As MSForms.ReturnBoolean)
If IsNumeric(txtPhone) Then
txtPhone.Text = Right(Format(txtPhone.Text, "###-###-####"), 12)
ElseIf txtPhone = "###-###-####" Then
txtPhone.Text = ""
Else
MsgBox ("Type in a valid Phone Number please")
txtPhone.SetFocus
End If
End Sub
Jay Freedman - 18 Jun 2007 20:08 GMT
Instead of trying to use SetFocus, set the cancel parameter to True:
Else
MsgBox ("Type in a valid Phone Number please")
cancel = True
End If
That way, the cursor won't leave the text box, so you don't have to send it
back.

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
> Hi,
>
[quoted text clipped - 18 lines]
>
> End Sub
Elaine - 18 Jun 2007 20:21 GMT
Thanks Jay, I actually found that in a posting from last year ;) and I
totally changed the Sub to include other checking as well.
> Instead of trying to use SetFocus, set the cancel parameter to True:
>
[quoted text clipped - 28 lines]
> >
> > End Sub