Link to home
Create AccountLog in
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!!!!
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

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

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
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.
Avatar of Computer Guy
Computer Guy

ASKER

I tried

update GetNumbersOnly([Phone])
From tablex

and it came back with an error in my update statement
what is the name of your table? field name for Phone?
contacts and w_phone
try this, backup table contacts first

update contacts
set w_phone =  GetNumbersOnly([w_Phone])
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

tried this and it failed, but worked with 1

update contacts
set w_phone =  GetNumbersOnly([w_Phone]),
set h_phone =  GetNumbersOnly([h_Phone])
this is the correct syntax

update contacts
set w_phone =  GetNumbersOnly([w_Phone]),
h_phone =  GetNumbersOnly([h_Phone])
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, "###-###-####")
here is the code

If Len(numOnly) = 10 Then
GetNumbersOnly = Format(numOnly, "###-###-####")
Else
GetNumbersOnly =numOnly
end if
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
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account