assessing a string value to be a phone number

S

Southern at Heart

I need to check to see if strPhone is a phone number with this format:
xxx-xxx-xxxx
It has to be this way or it won't work in the code further down the line
(which I can't change)

I can remove spaces from the beginning and end of it if they exist using
Left & Right fucntions. But is there a way to verify that the 4th and 8th
char is the hypen, and that all the other chars are digits?
many thanks,
SouthernAtHeart
 
J

John Spencer

IF Trim(StrPhone) Like "###-###-####" Then


'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
 
S

Southern at Heart

WOW! That's EASY! THANKS! This looks like a statement from a query, but
it'll work in VBA?
thanks so much!
SatH
 
L

Linq Adams via AccessMonster.com

Sure, you can use something like this, in VBA code

Private Sub strTelephone_BeforeUpdate(Cancel As Integer)
If Not (Trim(strTelephone) Like "###-###-####") Then
MsgBox "This is Not a Valid Phone Number!"
Cancel = True
End If
End Sub

to alert users that the number they entered is not a valid phone number.
 
L

Linq Adams via AccessMonster.com

Sorry, for your object name strPhone, that should be:

Private Sub strPhone_BeforeUpdate(Cancel As Integer)
If Not (Trim(strPhone) Like "###-###-####") Then
MsgBox "This is Not a Valid Phone Number!"
Cancel = True
End If
End Sub
 
Top