Hi All,
Environment is VBA code in Access 2007.
I am having some problems performing a find and replace on text that originates from a memo field in a table. The code is supposed to take internal document tracking numbers and replace them with simpler document number in reports for external clients, so the search string, or 'delimiter' is ~10 characters depending on document name.
I have used several methods involving the VBA functions: InStr; Split; and Replace. None reliably find the Search String within the test to be searched. ie. The find / replace methods work on some rich text fields, but not those that originate from memo fields.
I have tried passing variable ByVal into the find replace function, using String variables, both local and global, putting the text to be searched into text boxes on a form and processing them there before returning them to the table field and nothing I can see works.
Example memo text snippet:
"<div><strong>W at‐1.A.1: </strong>Complete d calculator. The shower usage calculation is adjusted according to bla bla bla.<br><strong >Wat‐1.A.2: </strong>drawings showing all installed fixtures</div>"
Searching for "Wat‐1.A.1"
Replacing with "Wat-1.1"
Example of function used to find replace:
Or with a different function via text boxes on form:
Similarly the InStr function always returned 0 even when it was clear that there was more than one occurance of the search string within th text to be searched.
Environment is VBA code in Access 2007.
I am having some problems performing a find and replace on text that originates from a memo field in a table. The code is supposed to take internal document tracking numbers and replace them with simpler document number in reports for external clients, so the search string, or 'delimiter' is ~10 characters depending on document name.
I have used several methods involving the VBA functions: InStr; Split; and Replace. None reliably find the Search String within the test to be searched. ie. The find / replace methods work on some rich text fields, but not those that originate from memo fields.
I have tried passing variable ByVal into the find replace function, using String variables, both local and global, putting the text to be searched into text boxes on a form and processing them there before returning them to the table field and nothing I can see works.
Example memo text snippet:
"<div><strong>W at‐1.A.1: </strong>Complete d calculator. The shower usage calculation is adjusted according to bla bla bla.<br><strong >Wat‐1.A.2: </strong>drawings showing all installed fixtures</div>"
Searching for "Wat‐1.A.1"
Replacing with "Wat-1.1"
Example of function used to find replace:
Code:
Public Function FindReplace(ByVal fStr, ByVal rStr, ByVal sStr As String) As String
Dim varSplit As Variant
Dim i As Integer
If fStr = rStr Then
Exit Function
End If
Do While InStr(1, sStr, fStr, 1) <> 0
varSplit = Split(sStr, fStr)
sStr = varSplit(0) & rStr & varSplit(1)
i = i + 1
Loop
Debug.Print ("Number of replacements: " & i & ". Search String: " & fStr)
FindReplace = sStr
End Function
Code:
Public Function FR_vForm(ByVal fStr, ByVal rStr, ByVal sStr As String)
Dim i As Integer
Dim tStr As String
tStr = ""
With Forms![Cover Sheet Generator]
.text_search = sStr
.text_find = fStr
.text_replace = rStr
i = 0
Do While .text_search <> tStr
tStr = .text_search
.text_search = Replace(.text_search, fStr, rStr)
i = i + 1
Loop
.text_output = FindReplace(fStr, rStr, sStr)
FR_vForm = .text_search
End With
End Function
Comment