Cleaning up Special Characters - Access

I'm importing phone numbers from a web source into Access.  Can someone help me to strip out the special characters and spaces and then format as:


Who is Participating?
dqmqConnect With a Mentor Commented:
Personally, I would store a 10-digit string without formatting and restrict it to acceptable digits. You may even need a longer string if you accept international numbers.  When collecting numbers, it's easy to validate a 10-digit number. (or a 3-digit area code and a 7-digit number).  It's very easy to apply the formatting to forms and reports if you are starting with proper numbers in your data.  It's also very important to validate inputs as close to the source as possible and avoid cleansing efforts like this, which are at best "fuzzy".

I would create a public function and then use it in VBA or in an update query as follows:

Update yourtable
  set phone = phonecleanup(phone)

Cannot give you too many specifics for the function as it depends so much on the diversity and patterns found in your phone number inputs. However, here is a simplistic function that should give you the idea.

Puplic Function PhoneCleanup(Phone as String) as String
Dim I As Integer
'remove all but digits
For I = 1 To Len(phone)
  If Not IsNumeric(Mid(phone, I, 1)) Then Mid(phone, I, 1) = "X"
phone = Replace(phone, "X", "")
'truncate to 10 digits
phone = Left(phone, 10)
'extend to 10 digits
phone = Right(Space(10) & phone, 10)
End Sub

Rey Obrero (Capricorn1)Commented:
place this function in a regular module

function cleanField(strPhone) as string
dim vChar as string, j as integer

if strPhone & ""="" then cleanfield="" :exit function

for j=1 to len(strPhone)
      if instr(vChar,mid(strPhone,j,1)) then
          cleanfield=cleanfield & mid(strPhone,j,1)
     end if


end function

to use in a query

update tableName
set [Phone]= fromat(cleanfield([Phone]),"(###)###-####")
where [Phone]<> null

post back for result
OnsiteSupportAuthor Commented:
Thank you.  Unfortunately, I have no control over the code that's generating the inputs. (Need an application that zaps the people who are sending me the data) :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.