ordendelfai
Technical User
Access 97
I was looking for a quick and easy function in Access VBA (like excels Replace() function) that will look at a string value I pass to it, and replace all searched for values with another value. I couldn't find anything, so hacked the below code together real quick. It does the job, but I am wondering if there is a more efficient way and perhaps less code to achieve the same results. The below code should cut/paste into a module and can be run.
RoyVidar, your example in this Thread gave me the idea to put this together (just to give credit).
~Joel
As common courtesy and to help other members who might find your thread helpful in the future, please respond if the advice given resolved your issue.
I was looking for a quick and easy function in Access VBA (like excels Replace() function) that will look at a string value I pass to it, and replace all searched for values with another value. I couldn't find anything, so hacked the below code together real quick. It does the job, but I am wondering if there is a more efficient way and perhaps less code to achieve the same results. The below code should cut/paste into a module and can be run.
RoyVidar, your example in this Thread gave me the idea to put this together (just to give credit).
Code:
Sub ReplaceText()
Dim strFindThis 'Holds the Variable of what to Find
Dim strFindThis_Size 'Holds the size of the variable to find
Dim strInThis 'Holds the object to search in
Dim strInThis_Temp 'Holds the original, and then new looped string value of the object to search in
Dim strFoundIn As Long 'Holds the numerical value of where the value being searched was found
Dim strToThis 'Holds the variable of what to change the variable found to
Dim strToThis_Size
strFindThis = "Bob"
strFindThis_Size = Len(strFindThis)
strInThis = "His name is Bob Smith. Bob and his friend carpool to work."
strInThis_Temp = strInThis
strToThis = "Jeremy"
strToThis_Size = Len(strToThis)
MsgBox strInThis 'Displays the original text of the object to search in
Do Until InStr(strInThis, strFindThis) = 0
strFoundIn = InStr(strInThis, strFindThis)
strInThis = Mid$(strInThis, 1, InStr(strInThis, strFindThis) - 1) & strToThis
strInThis = strInThis & Mid$(strInThis_Temp, strFoundIn + strFindThis_Size)
strInThis_Temp = strInThis 'Change the temp full string to the changed string to prepare for next search
MsgBox strInThis 'Shows the changes as they occur
Loop
End Sub
~Joel
As common courtesy and to help other members who might find your thread helpful in the future, please respond if the advice given resolved your issue.