Avatar of Computer Guy
Computer Guy
 asked on

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!!!!
Microsoft AccessVBA

Avatar of undefined
Last Comment
Rey Obrero (Capricorn1)

8/22/2022 - Mon
Joe Howard

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)

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 (EE MVE )

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Computer Guy

ASKER
I tried

update GetNumbersOnly([Phone])
From tablex

and it came back with an error in my update statement
Rey Obrero (Capricorn1)

what is the name of your table? field name for Phone?
Computer Guy

ASKER
contacts and w_phone
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rey Obrero (Capricorn1)

try this, backup table contacts first

update contacts
set w_phone =  GetNumbersOnly([w_Phone])
Rey Obrero (Capricorn1)

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 Guy

ASKER
tried this and it failed, but worked with 1

update contacts
set w_phone =  GetNumbersOnly([w_Phone]),
set h_phone =  GetNumbersOnly([h_Phone])
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Rey Obrero (Capricorn1)

this is the correct syntax

update contacts
set w_phone =  GetNumbersOnly([w_Phone]),
h_phone =  GetNumbersOnly([h_Phone])
Computer Guy

ASKER
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)

here is the code

If Len(numOnly) = 10 Then
GetNumbersOnly = Format(numOnly, "###-###-####")
Else
GetNumbersOnly =numOnly
end if
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Computer Guy

ASKER
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.
ASKER CERTIFIED SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.