?
Solved

Cleaning up Special Characters - Access

Posted on 2011-09-13
3
Medium Priority
?
331 Views
Last Modified: 2012-05-12
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:

(###)###-####

Thanks
0
Comment
Question by:OnsiteSupport
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36529942
place this function in a regular module

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

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

next

end function


to use in a query

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




post back for result
0
 
LVL 42

Accepted Solution

by:
dqmq earned 2000 total points
ID: 36530138
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"
Next
phone = Replace(phone, "X", "")
'truncate to 10 digits
phone = Left(phone, 10)
'extend to 10 digits
phone = Right(Space(10) & phone, 10)
End Sub



0
 

Author Closing Comment

by:OnsiteSupport
ID: 36530257
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) :)
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

752 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