Find specific lenght of string of numbers in Access

KrazyAl

Board Regular
Joined
Sep 28, 2007
Messages
75
Hello

After unsuccessful searches, I am posting this here. I have a database that may contain on certain occasion credit card information. I need to find these numbers and rename them. So any number that looks like 1234 5678 9123 4567 or 1234-5678-9123-4567 needs to look like xxxx xxxx xxxx xxxx

This string would be in a text field in my databases. therefore might show up anywhere in that field.

A macro would be fine, even a query function/formulas is good. I can even use Excel if that makes it easier :)

Thank
 
Last edited:
If it is possible to automate a find and replace, I am Ok with that.

Once the data is run and my file extracted (with the numbers removed) the data is destroyed (table is deleted), and new data is updated the following week. This is a weekly database and previous data is always deleted
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
is this to be done in access or excel, i thought it was to be a permanent change in the database
 
Upvote 0
With the usual caveats (matching strings that are somewhat variable is always tricky), James is probably correct that regex is a promising solution.

Assuming in Access, here is a function that can go in a standard module. This is meant to replace 16 digit credit card numbers that may have a hyphen or space (or two spaces) between each set of 4 digits. Already this is probably insufficient since some credit cards (I think) actually have 15 digits. So the regex could use a lot of work. You might find a better pattern googling.

But for demonstration.
Code:
Public Function XOutCreditCardNumbers(ByVal arg) As String

Dim re As Object


Set re = CreateObject("VBScript.RegExp")

With re
    .Global = True
    .IgnoreCase = True
    .Multiline = True
    .Pattern = "\d{4}( |\-){0,2}\d{4}( |\-){0,2}\d{4}( |\-){0,2}\d{4}( |\-){0,2}"
End With

If re.Test(arg) Then
    arg = re.Replace(arg, "XXXX-XXXX-XXXX-XXXX")
End If

XOutCreditCardNumbers = arg

End Function

Demonstration:
Code:
Sub foo()
Dim s As String
Dim t As String
     s = "Excepteur sint occaecat 1234567890123456 cupidatat non proident, sunt in 1234 5678 9012 3456 culpa qui 1234-5678-9012-3456officia deserunt1234  5678 9012  3456 mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit."
     t = XOutCreditCardNumbers(s)
     
     Debug.Print s
     Debug.Print t

End Sub

You could use in a query simply like another function:
Select MyField, XOutCreditCardNumbers(MyField) from MyTable
 
Upvote 0

Forum statistics

Threads
1,221,792
Messages
6,161,997
Members
451,735
Latest member
Deasejm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top