Solved

Way to strip out all non-numer characters in Access Query

Posted on 2009-07-01
3
556 Views
Last Modified: 2012-05-07
I have a basic query that is trying to clean up a TIN/SSN field.  right now there are many non-numeric characters that I'd like to clean.  The below works well for mos of the characters, but I also have several cases where the users put in words (e.g. SSN:"123-45-6789").  I'm looking for a quick way to remove ALL non-numeric characters.  

NewTaxID: Replace(Replace(Replace(Replace(Replace([Tax_Id],"-",""),":","")," ",""),"_",""),"/","")
NewTaxID: Replace(Replace(Replace(Replace(Replace([Tax_Id],"-",""),":","")," ",""),"_",""),"/","")

Open in new window

0
Comment
Question by:BBlu
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 50 total points
ID: 24759391
Hello BBlu,

Add the code below to a regular VBA module, and then use this in the query designer:

NewTaxID: RegExpReplace([Tax_ID], "[^0-9]", "")

Regards,

Patrick
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True) 
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
    Set RegX = Nothing
    
End Function

Open in new window

0
 

Author Comment

by:BBlu
ID: 24759408
Wow!  I almost feel guilty that it was that easy!  Now I need to dive into the VBA module to see how/why it works.  Thanks, Patrick!  
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24759522
Glad to help!  BTW, you may want to try the version below.  If you have a lot of records to sort through, it
may run marginally faster.

Regular Expressions is an amazingly flexible and fast way to parse text.  Many people use it in Perl, and it is
also implemented in VBScript.  This UDF instantiates the RegExp class from VBScript and exposes it in
VBA.  I use it, and a similar RegExp-powered "find" function, very frequently.

The pattern string [^0-9] means "any character EXCEPT 0 through 9", and then in the formula I wrote any
such character is replaced with a zero-length string.
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True) 
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
        
End Function

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Outlook Free & Paid Tools
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

786 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