Remove non numeric characters

Hi,

I have a phone field in my access DB.

there is a variety of formats. Sometimes there are / - ( ). I just want to remove any characters that are not 0-9

Also, I want to find out what what numbers are not 10 digits long.

I want to add 123 if the number is not 10 digits and then format it as ###-###-####

Thanks!!!!
LVL 3
Computer GuyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
The first part of your request can be accomplished by using the following function:
Function EEDemo(varInput As Variant)
    Dim strTemp As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "\D+"
        .Global = True
        strTemp = .Replace(varInput, "")
    End With
    EEDemo = strTemp
End Function

Open in new window


As far as the second part is concerned, I'm not sure what you mean. Are you only looking to add the area code if it is missing?
If that is the case this function will do the job.
Function EEDemo(varInput As Variant)
    Dim strTemp As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "\D+"
        .Global = True
        strTemp = .Replace(varInput, "")
    End With
    If Len(strTemp) = 7 Then
        EEDemo = Format("123" & strTemp, "###-###-####")
    Else
        EEDemo = strTemp
    End If
End Function

Open in new window

Rey Obrero (Capricorn1)Commented:
to remove those characters, you can use the Replace() function in a query

select replace(replace(replace(replace([Phone],"/",""),"(",""),")",""),"-","")
from table

or use a VBA function
place this codes in a regular module
Function GetNumbersOnly(strIn As String)
Dim j, numOnly
If strIn = "" Then Exit Function
For j = 1 To Len(strIn)
    If IsNumeric(mid(strIn, j, 1)) Then
        numOnly = numOnly & mid(strIn, j, 1)
    End If
Next
GetNumbersOnly = numOnly
End Function

Open in new window

then use the function in your query

select GetNumbersOnly([Phone])
From tablex
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I think you need to "walk the string" and pick out the items which are numeric:

Dim i As Integer
Dim res As String
For i=0 to Len(YourString) - 1
  If IsNumeric(Mid(YourString, i, 1)) Then
    res = res & Mid(YourString, i, 1)
  End If
Next i

i= 1
If Len(res) < 10 Then
  Do until Len(res) = 10
    res = Cstr(i) & res
    i = i+1
  Loop
End If

Not sure about the formatting - you generally do that on a Form.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Computer GuyAuthor Commented:
I tried

update GetNumbersOnly([Phone])
From tablex

and it came back with an error in my update statement
Rey Obrero (Capricorn1)Commented:
what is the name of your table? field name for Phone?
Computer GuyAuthor Commented:
contacts and w_phone
Rey Obrero (Capricorn1)Commented:
try this, backup table contacts first

update contacts
set w_phone =  GetNumbersOnly([w_Phone])
Rey Obrero (Capricorn1)Commented:
to add the formatting use this modified function

Function GetNumbersOnly(strIn As String)
Dim j, numOnly
If strIn = "" Then Exit Function
For j = 1 To Len(strIn)
    If IsNumeric(Mid(strIn, j, 1)) Then
        numOnly = numOnly & Mid(strIn, j, 1)
    End If
Next
If Len(numOnly) < 10 Then
GetNumbersOnly = "123-" & Format(numOnly, "###-####")
Else
GetNumbersOnly = Format(numOnly, "###-###-####")
End If
End Function

Open in new window

Computer GuyAuthor Commented:
tried this and it failed, but worked with 1

update contacts
set w_phone =  GetNumbersOnly([w_Phone]),
set h_phone =  GetNumbersOnly([h_Phone])
Rey Obrero (Capricorn1)Commented:
this is the correct syntax

update contacts
set w_phone =  GetNumbersOnly([w_Phone]),
h_phone =  GetNumbersOnly([h_Phone])
Computer GuyAuthor Commented:
Ok, thought this was easier to do on my own:

For this part, if the number is < 10 or > 10, just leave it alone. Haha, then that should be it!

If it is 10 exactly, then format

If Len(numOnly) < 10 Then
GetNumbersOnly = "123-" & Format(numOnly, "###-####")
Else
GetNumbersOnly = Format(numOnly, "###-###-####")
Rey Obrero (Capricorn1)Commented:
here is the code

If Len(numOnly) = 10 Then
GetNumbersOnly = Format(numOnly, "###-###-####")
Else
GetNumbersOnly =numOnly
end if
Computer GuyAuthor Commented:
Last part,

Function GetNumbersOnly(strIn As String)
Dim j, numOnly
If strIn = "" Then Exit Function
For j = 1 To Len(strIn)
    If IsNumeric(Mid(strIn, j, 1)) Then
        numOnly = numOnly & Mid(strIn, j, 1)
    End If

How can I keep the character x?

Sometimes people put their number in and have x for extension.
Rey Obrero (Capricorn1)Commented:
this will definitely be my last post, if there are additional revision, just post another Question..

For j = 1 To Len(strIn)
    If IsNumeric(Mid(strIn, j, 1)) Then
        numOnly = numOnly & Mid(strIn, j, 1)

        elseif Mid(strIn, j, 1)="x" then

         numOnly = numOnly & Mid(strIn, j, 1)

    End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.