Solved

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

Posted on 2009-07-01
3
554 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now