?
Solved

Scan A String

Posted on 2003-03-11
8
Medium Priority
?
209 Views
Last Modified: 2010-05-01


Hi

Does anyone know how can i scan a string/text for Patterns like
phone numbers.

Thanks.


0
Comment
Question by:yanivnachumi1
7 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 8113518
msgbox "this is a phone number 1-800-456-3333" like "*###[-]####*"
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8113535
The key is the like operator.  You can match more than one separator character in the braces.
0
 

Expert Comment

by:igfp
ID: 8126515
Sure, here's a code that will do the trick. You want to scan a string. Let's say it's a text box called Text1.
First pass that text1 to a string. then analyse each one by one. a command will do this. I don't know what phone numbers you want but american ones are like this? : 1-800-456-3333?? If they are you know they have to have 14 chars. So char number 2, 5 and 9 are "-". We will test this at the begining.

Private Sub cmdVerify_Click()

Dim PhoneNum as string
Dim Temp as String * 1

PhoneNum$ = Text1.text
If Mid(PhoneNum, 2, 1) <> "-" or_
Mid(PhoneNum, 5, 1) <> "-" or_
Mid(PhoneNum, 9, 1) <> "-" or_
len(PhoneNum) <> "15" then
goto error:
Else
End if
' So far we know that there are 15 chars and char 2,
' 5 and 9 are "-", so far so good. Now let's see
' if other chars are numbers!
' For this ww'll use ascii codes from 49 to 57

if Mid(PhoneNum, 5, 1)<"49" and Mid(PhoneNum, 5, 1)>"57" then
goto error:
Else
End if

For i%= 3 to 5
    Temp$ = Mid(AuthorInicial, i%, 1)
    if Temp$ <"49" and Temp$ >"57" Then
     Goto Error:
     Else
Next i%

For i%= 7 to 9
    Temp$ = Mid(AuthorInicial, i%, 1)
    if Temp$ <"49" and Temp$ >"57" Then
     Goto Error:
     Else
Next i%

For i%= 11 to 14
    Temp$ = Mid(AuthorInicial, i%, 1)
    if Temp$ <"49" and Temp$ >"57" Then
     Goto Error:
     Else
Next i%

msgbox "Phone Number Is Correct!"
'Here you should change the msgbox to
'whatever you want to do with the phone
'number (you already know that it's correct)
Exit Sub ' I do this because otherwise it'll
'read the error message anyway!

error:
    msgbox "Phone Number is not correct"
End Sub


Hope this helped!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:igfp
ID: 8127190
instead of 49, 57 use <"9" and > "1" That's what I meant. I forgot to asc so change those values for these and you shall be fine!
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 200 total points
ID: 8128642
igfp:  You could perform the same test using this code:

If PhoneNum Like "#[-]###[-]###[-]####" then
  msgbox "Number is a phone number"
else
  msgbox "Not a phone number"
endif

Adding asterisks to the beginning and end allows you to check if there is a phone number within the string as in my example.  You could also allow spaces for separators, thus:

If PhoneNum Like "#[- ]###[- ]###[- ]####" then
  msgbox "Number is a phone number"
else
  msgbox "Not a phone number"
endif

The like operator is fairly powerful, allowing wildcard matching for multiple or single characters or digits.  It is also very quick both for coding and execution.
0
 

Expert Comment

by:CleanupPing
ID: 9056432
yanivnachumi1:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9097096
yanivnachumi1, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question