Is it possible replace the value with regex in batch?

sleepy11

New member
Local time
Today, 19:45
Joined
May 27, 2024
Messages
7
I'm newbee to MS Access and just got a request to add the comma to the value of the address column, not just change the format of view.

Before, the value is like "1234 johndon street",
After, the value should like "1,234 johndon street". (with an extra comma after "1")

And there are thousands of records to change.

Is it possible that Access can do it with the "find/replace" dialog?
 
Hi. Welcome to AWF!

Regex is pretty powerful, so what you might be possible. The only tricky part is figuring out the appropriate pattern to use.
 
So what would you want if the address was 876 Carmarthen Rd ?
 
Hi. Welcome to AWF!

Regex is pretty powerful, so what you might be possible. The only tricky part is figuring out the appropriate pattern to use.
Thanks for the reply. I understood regex, but it seems that Access doesn't support it in the "find/replace" dialog, or only support some wildcards as per below page:

Use wildcards in queries and parameters in Access​


Could you please provide an example for my specific question above?
 
I do not know regex.
So I would create a function that would get the value of the front of the address and format as your requirement, then append the rest of the address, and update the field with the function output.
This would not work if you have addresses like Willhelstrasse 12345
 
I do not know regex.
So I would create a function that would get the value of the front of the address and format as your requirement, then append the rest of the address, and update the field with the function output.
This would not work if you have addresses like Willhelstrasse 12345
Thanks for the detail explication. But base on my current Access knowledge base, I would like focus on the "find/replace" dialog and persue the function a little bit later.:(
 
Because the find/replace dialog doesn't help you to directly alter a table's contents in this particular way, you will need to do one of two things. Either (A) write code to step through a recordset to apply a special parse/reformat function or (B) write a public function that "wraps" the code to apply the parsing/formatting.

In either case, the code would have to first decide whether the current number actually qualifies for this reformatting, and then it would have to reformat the number string.

IF you have a public function that tests and returns the correct string, you can write an SQL query to step through and update the records. I would choose the SQL UPDATE method ONLY because if you did it via recordset, that is all done in VBA and unfortunately, VBA is a LOT slower than performing recordset operations through SQL. For thousands of records, VBA might be painfully slow. I have a related case where I get a file in UTF-8 format and have to convert it to ANSI text and that little problem takes forever. Not that hard to correct the UTF-8 extended character to something usable, but it takes forever to find the problem in the first place. So a raw VBA-only approach isn't so good.

A point was raised by Gasman that not all postal designations should get the "comma" treatment. For example, USA ZIP codes are always 5 digits for which that comma would probably screw up the automated postal sorting machines. Even worse, the ZIP+4 codes have a 5-digit and a 4-digit number together and NEITHER should receive the "comma' treatment. This means that your parsing approach is going to be important as well.
 
Thanks for the reply. I understood regex, but it seems that Access doesn't support it in the "find/replace" dialog, or only support some wildcards as per below page:

Use wildcards in queries and parameters in Access​


Could you please provide an example for my specific question above?
The built-in Find and Replace probably doesn't support Regular Expressions. I was talking more about you having to create your own function to do the replacing for you. I have demonstrated a few times how to extract values using RegEx. I'll have to find a way to show how to use it for replacements next.
 
The o/p said there are thousands of records to change?
Is that actually the records that need to be changed or just the whole recordset?
I would be only working on the records that actually need to be changed?
 
sleepy11,

How consistent is your data? Are your addresses formatted in a standard/consistent manner?
Your request is interesting, seems a local requirement rather than a Canada Post formatting need.
 
I have never seen an address formatted as such.
I have friends in Keithville whose house number is 11472.
 
I have never seen an address formatted as such.
I have friends in Keithville whose house number is 11472.
The norm in the US is the same. Thousand separators are usually are not normally used for addresses
 
Here in the UK, our addresses do not generally go that high. TBH I have never seen one in the UK.
 
sleepy11,

How consistent is your data? Are your addresses formatted in a standard/consistent manner?
Your request is interesting, seems a local requirement rather than a Canada Post formatting need.

Actually, it is a question in the MS Access certificate book. The data, which need to be updated, are in the columne "ProAdresse".

1716829504863.png
 
Because the find/replace dialog doesn't help you to directly alter a table's contents in this particular way, you will need to do one of two things. Either (A) write code to step through a recordset to apply a special parse/reformat function or (B) write a public function that "wraps" the code to apply the parsing/formatting.

In either case, the code would have to first decide whether the current number actually qualifies for this reformatting, and then it would have to reformat the number string.

IF you have a public function that tests and returns the correct string, you can write an SQL query to step through and update the records. I would choose the SQL UPDATE method ONLY because if you did it via recordset, that is all done in VBA and unfortunately, VBA is a LOT slower than performing recordset operations through SQL. For thousands of records, VBA might be painfully slow. I have a related case where I get a file in UTF-8 format and have to convert it to ANSI text and that little problem takes forever. Not that hard to correct the UTF-8 extended character to something usable, but it takes forever to find the problem in the first place. So a raw VBA-only approach isn't so good.

A point was raised by Gasman that not all postal designations should get the "comma" treatment. For example, USA ZIP codes are always 5 digits for which that comma would probably screw up the automated postal sorting machines. Even worse, the ZIP+4 codes have a 5-digit and a 4-digit number together and NEITHER should receive the "comma' treatment. This means that your parsing approach is going to be important as well.
Thanks for the explanation. As per my understanding, it is for the door number. Since it is a question in the MS Access certificate book, which doesn't touch the function part, I don't have knowledge on it, so far. :(
 
Well, your data is inconsistent.
Record 6 has no comma.
 
While you can FIND records using a wildcard, you cannot REPLACE using a wildcard.
Find: ####*
(then repeat for 5, 6, etc. digits)
To do a find and replace, RegEx code would be most efficient, but not super easy for a rookie.
RegExp is in "Microsoft VBScript Regular Expressions 5.5" library. You'll need to set that reference in VBA window > Tools > References.
RegEx syntax is here: Regular Expression Syntax (Scripting) | Microsoft Learn
 
If the numbers are always at the start of the address you can use the val and replace functions

str="1246, Cosworth Place"
replace(str,val(str),format(val(str),"#,##0"))

in the immediate window
?replace("1246, Cosworth Place",val("1246, Cosworth Place"),format(val("1246, Cosworth Place"),"#,##0"))
1,246, Cosworth Place
 

Users who are viewing this thread

Back
Top Bottom