Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Validating Text in texbox on a Form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Elaine - 18 Jun 2007 19:50 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.