Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access VBA | How to replace parts of a string with another string

Tags:

vba

ms-access

I am trying to create a piece of code that replaces one word with another. Example: Replace Avenue with Ave and North with N. I am using MS Access, I could use SQL REPLACE Function but I want to do this in VBA using Access module so that I can attached the function to other column.

I am not sure where to start with this, so any input will be greatly appreciated.

Guy

like image 254
Asynchronous Avatar asked Dec 18 '11 06:12

Asynchronous


People also ask

How do I replace part of a string in VBA?

Use the VBA Replace function to replace a substring of characters in a string with a new string. VBA Replace is similar to the Excel SUBSTITUTE function; both can be used to replace a portion of a string with another.

Is there a Replace function in VBA?

The VBA REPLACE function is listed under the text category of VBA functions. When you use it in a VBA code, it replaces a substring from string with a new sub-string. In simple words, you can use REPLACE to replace a part of text with another text and it returns that new text in the result.

How do you replace text in an Access query?

Keyboard shortcut Press CTRL+F. The Find and Replace dialog box appears. To find data, in the Find and Replace dialog box, click the Find tab. To run a find-and-replace operation, click the Replace tab.

Is there a substitute function in access?

The Microsoft Access Replace function replaces a sequence of characters in a string with another set of characters (a number of times).


2 Answers

Use Access's VBA function Replace(text, find, replacement):

Dim result As String  result = Replace("Some sentence containing Avenue in it.", "Avenue", "Ave") 
like image 58
Mitch Wheat Avatar answered Oct 28 '22 18:10

Mitch Wheat


I was reading this thread and would like to add information even though it is surely no longer timely for the OP.

BiggerDon above points out the difficulty of rote replacing "North" with "N". A similar problem exists with "Avenue" to "Ave" (e.g. "Avenue of the Americas" becomes "Ave of the Americas": still understandable, but probably not what the OP wants.

The replace() function is entirely context-free, but addresses are not. A complete solution needs to have additional logic to interpret the context correctly, and then apply replace() as needed.

Databases commonly contain addresses, and so I wanted to point out that the generalized version of the OP's problem as applied to addresses within the United States has been addressed (humor!) by the Coding Accuracy Support System (CASS). CASS is a database tool that accepts a U.S. address and completes or corrects it to meet a standard set by the U.S. Postal Service. The Wikipedia entry https://en.wikipedia.org/wiki/Postal_address_verification has the basics, and more information is available at the Post Office: https://ribbs.usps.gov/index.cfm?page=address_info_systems

like image 42
Dave 2.71828 Avatar answered Oct 28 '22 19:10

Dave 2.71828