R
Robert Neville
I had an idea for a replace function that would iterate through a
table matching and replacing data. The novelty of the function
relates to using regular expressions and placing all find values in a
separate table. The function would read through one table and change
values in another table. Below you will find my rough draft. I do not
know if this approach is efficiency, yet it seems to work. I have not
intensively tested it. I would like to get the thoughts of some
developers before implementing this approach.
Public Function fnReplace()
Dim X As Integer
Dim dbs As Database
Dim rstPatterns As Recordset
Dim rst As Recordset
Dim strFind As String
Dim strReplace As String
Dim blnMatchCase As Boolean
Set dbs = CurrentDb
Set rstPatterns = dbs.OpenRecordset("tblReplace")
Dim objReg As VBScript_RegExp_55.RegExp
Set objReg = New VBScript_RegExp_55.RegExp
With rstPatterns
rstPatterns.MoveFirst
'loop thru each row
Do While Not rstPatterns.EOF
Set rst = dbs.OpenRecordset(rstPatterns!tables)
With rst
'loop thru each col
For X = 0 To rst.Fields.Count - 1
If rst.Fields(X).Name = rstPatterns!FieldName Then
Do While Not rst.EOF
' Set pattern.
objReg.PATTERN = rstPatterns!PATTERN
blnMatchCase = rstPatterns!Case
' Set case insensitivity
objReg.IgnoreCase = Not (blnMatchCase)
objReg.Global = True
strReplace = rstPatterns!Replace
rst.Edit
rst.Fields(X) = objReg.Replace(rst.Fields(X),
strReplace)
rst.Update
.MoveNext
Loop
Set objReg = Nothing
strReplace = ""
End If
Next
End With
.MoveNext
Loop
End With
dbs.Close
Set objReg = Nothing
Set rstPatterns = Nothing
Set rst = Nothing
Set dbs = Nothing
End Function
table matching and replacing data. The novelty of the function
relates to using regular expressions and placing all find values in a
separate table. The function would read through one table and change
values in another table. Below you will find my rough draft. I do not
know if this approach is efficiency, yet it seems to work. I have not
intensively tested it. I would like to get the thoughts of some
developers before implementing this approach.
Public Function fnReplace()
Dim X As Integer
Dim dbs As Database
Dim rstPatterns As Recordset
Dim rst As Recordset
Dim strFind As String
Dim strReplace As String
Dim blnMatchCase As Boolean
Set dbs = CurrentDb
Set rstPatterns = dbs.OpenRecordset("tblReplace")
Dim objReg As VBScript_RegExp_55.RegExp
Set objReg = New VBScript_RegExp_55.RegExp
With rstPatterns
rstPatterns.MoveFirst
'loop thru each row
Do While Not rstPatterns.EOF
Set rst = dbs.OpenRecordset(rstPatterns!tables)
With rst
'loop thru each col
For X = 0 To rst.Fields.Count - 1
If rst.Fields(X).Name = rstPatterns!FieldName Then
Do While Not rst.EOF
' Set pattern.
objReg.PATTERN = rstPatterns!PATTERN
blnMatchCase = rstPatterns!Case
' Set case insensitivity
objReg.IgnoreCase = Not (blnMatchCase)
objReg.Global = True
strReplace = rstPatterns!Replace
rst.Edit
rst.Fields(X) = objReg.Replace(rst.Fields(X),
strReplace)
rst.Update
.MoveNext
Loop
Set objReg = Nothing
strReplace = ""
End If
Next
End With
.MoveNext
Loop
End With
dbs.Close
Set objReg = Nothing
Set rstPatterns = Nothing
Set rst = Nothing
Set dbs = Nothing
End Function