0

I'm working on a project cleaning up addresses and need to create about 40+ rules for fixing them. What I need is a Microsoft Access file with a query that calls a visual basic module to run multiple REPLACE commands on a string of data.

Example: Let's say the data strings look like this

  • 123 Fake Str.
  • 345 Fake Street

I want the visual basic module to be able to do

  • Replace "Str." with "ST"
  • Replace "Street" with "ST"
  • Replace "Road" with "RD"

and so on. Once I have that, I can copy the structure over to the actual data. However I'm at the limit of my knowledge on how to call a VBA function in a query, and how to get the VBA function to export a value that the query can read. Any help would be much appreciated

2
  • Is the data in fields of a table? And I'm hoping you are not suggesting that you need a UDF approach to this. Commented Aug 3, 2020 at 22:00
  • Yes, the fields are in a table. My challenge is that to do this in SQL would involve either 50 queries that feed sequentially, or function so deeply nested that I couldn't claw my way out. Commented Aug 3, 2020 at 22:06

1 Answer 1

2

I am not sure if I understand your problem right because it is a very simple task:

sql = "SELECT MyReplaceFunction([MyAddressField]) FROM MyTable"

put a public function in a module like this:

Public Function MyReplaceFunction(FieldValue As String) As String
    FieldValue  = Replace(FieldValue,"Str.","ST")
    FieldValue  = Replace(FieldValue,"Street","ST")
    FieldValue  = Replace(FieldValue,"Road","RD")
' continue like that with all your criteria
    MyReplaceFunction = FieldValue  
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

Oh my gosh thank you, this is going to save me the nightmare of nested replace functions or 50 separate replace queries, I was dreading having to do that
@RobertPatrician Just to make you aware, performance-wise and for debugging, this is not great. Also this is not changing the underlying data, but rather presenting "a cleaner form" of the data each time the data is presented. If you want to perform a clean up of the data, then I recommend you take a different approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.