Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Query to Remove Special Charaters 2

Status
Not open for further replies.

perrymans

IS-IT--Management
Nov 27, 2001
1,340
US
I am importing data and have records where the () and - in a phone number were typed into the field:

(555) 555-2234

Is there a way to do an update query that removes the ()- characters (and spaces) from the field?

I have the formatting taken care of in the field of the form.

Thanks. Sean.
 
You can either use nested Replace() functions or create your own function.

RuralGuy (RG for short) aka Allan Bunch MS Access MVP acXP winXP Pro
Please respond to this forum so all may benefit
 
Howdy perrymans . . .

In parallel with [blue]RuralGuy[/blue], try the following:
[ol][li]In a module in the [blue]modules window[/blue], copy/paste the following function
Code:
[blue]Public Function CorrectPhone(ByVal PhNum)
   PhNum = Replace(PhNum, "(", "")
   PhNum = Replace(PhNum, ")", "")
   PhNum = Replace(PhNum, "-", "")
   PhNum = Replace(PhNum, " ", "")
   CorrectPhone = PhNum
End Function[/blue]
[/li]
[li]In design view of the update query under your [blue]PhoneFieldName[/blue], set [blue]Updated To:[/blue] as follows:
Code:
[blue]CorrectPhone([[purple][B][I]YourPhoneFieldName[/I][/B][/purple]])[/blue]
[/li][/ol]

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
In a standard code module create the following function:
Code:
Public Function getPhoneNum(strPhone)
Dim i As Integer, x As String, S As String
If Trim(strPhone & "") = "" Then Exit Function
For i = 1 To Len(strPhone)
  x = Mid(strPhone, i, 1)
  If IsNumeric(x) Then S = S & x
Next
getPhoneNum = S
End Function

And now your update query:
Code:
UPDATE yourTable
SET [phone field] = getPhoneNum([phone field])
WHERE [phone field] Like '*[!0-9]*'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks AceMan and PH.

PHV's solution worked better because of the nulls contained in some of the records and it alos got rid all all characters that weren't numbers.

Very nice.

Thanks. Sean.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top