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
Solved

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

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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 …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

790 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