R
robert_neville
I am having trouble with my regular expression module. Apparently, MS
Access returns an object-defined error [5020]. The objective behind my
code involves iterating through a table with regular expression
patterns and replace arguments; then applying regular expression
object's replace method on a text file. The code follows below.
Please let me know if you have any ideas.
‘--------------------------------------------------------------------
Private Sub cmdReplace_Click()
‘--------------------------------------------------------------------
Dim dbsCurrent As DAO.Database
Dim strTableName As String
Dim strFileContents As String
Dim strSuffix As String
Dim strNewFileName As String
Set dbsCurrent = CurrentDb
If lstReplace.ListIndex < 0 Then
MsgBox "Please select a table"
lstReplace.SetFocus
Exit Sub
End If
If IsNull(Me!txtFile) Then
Beep
MsgBox "File name required."
Exit Sub
End If
If Len(Dir(Me!txtFile)) = 0 Then
Beep
MsgBox Me!txtFile & " file not found."
Exit Sub
End If
strTableName = lstReplace.Column(0, lstReplace.ListIndex)
strFileContents = FileContents(Me!txtFile)
strFileContents = fnFindandReplace(strTableName, strFileContents)
strSuffix = "_DELIMITED"
strNewFileName = Left(Me!txtFile, Len(Me!txtFile) - 4) & strSuffix
& ".txt"
Debug.Print "cmdReplace: NewFileName -> "; strNewFileName '
Debug Statement
intRet = OutputTextFile(strFileContents, strNewFileName)
End Sub
‘--------------------------------------------------------------------
Public Function fnFindandReplace(strTable As String, varFileContents
As Variant)
‘--------------------------------------------------------------------
Dim dbsCurrent As DAO.Database
Dim rstPatterns As DAO.Recordset
Dim strFind As String
Dim strReplace As String
Dim strFileContents As String
Dim blnMatchCase As Boolean
Set dbsCurrent = CurrentDb
Set rstPatterns = dbsCurrent.OpenRecordset(strTable)
' If IsNull(varFileContents) Then Exit Function
Dim objReg As VBScript_RegExp_55.RegExp
Set objReg = New VBScript_RegExp_55.RegExp
strFileContents = CStr(varFileContents)
With rstPatterns
rstPatterns.MoveFirst
'loop thru each row
Do While Not rstPatterns.EOF
If rstPatterns!Trigger = "Yes" Then
'loop thru each col
objReg.Pattern = rstPatterns!Pattern
blnMatchCase = rstPatterns!Case
' Set case insensitivity
objReg.IgnoreCase = Not (blnMatchCase)
objReg.Global = True
strReplace = Replace(rstPatterns!Replace, "|", "")
‘ERROR OCCURS BELOW
strFileContents = objReg.Replace(strFileContents,
strReplace)
strReplace = ""
End If
.MoveNext
Loop
End With
dbsCurrent.Close
Set objReg = Nothing
Set rstPatterns = Nothing
Set dbs = Nothing
End Function
Access returns an object-defined error [5020]. The objective behind my
code involves iterating through a table with regular expression
patterns and replace arguments; then applying regular expression
object's replace method on a text file. The code follows below.
Please let me know if you have any ideas.
‘--------------------------------------------------------------------
Private Sub cmdReplace_Click()
‘--------------------------------------------------------------------
Dim dbsCurrent As DAO.Database
Dim strTableName As String
Dim strFileContents As String
Dim strSuffix As String
Dim strNewFileName As String
Set dbsCurrent = CurrentDb
If lstReplace.ListIndex < 0 Then
MsgBox "Please select a table"
lstReplace.SetFocus
Exit Sub
End If
If IsNull(Me!txtFile) Then
Beep
MsgBox "File name required."
Exit Sub
End If
If Len(Dir(Me!txtFile)) = 0 Then
Beep
MsgBox Me!txtFile & " file not found."
Exit Sub
End If
strTableName = lstReplace.Column(0, lstReplace.ListIndex)
strFileContents = FileContents(Me!txtFile)
strFileContents = fnFindandReplace(strTableName, strFileContents)
strSuffix = "_DELIMITED"
strNewFileName = Left(Me!txtFile, Len(Me!txtFile) - 4) & strSuffix
& ".txt"
Debug.Print "cmdReplace: NewFileName -> "; strNewFileName '
Debug Statement
intRet = OutputTextFile(strFileContents, strNewFileName)
End Sub
‘--------------------------------------------------------------------
Public Function fnFindandReplace(strTable As String, varFileContents
As Variant)
‘--------------------------------------------------------------------
Dim dbsCurrent As DAO.Database
Dim rstPatterns As DAO.Recordset
Dim strFind As String
Dim strReplace As String
Dim strFileContents As String
Dim blnMatchCase As Boolean
Set dbsCurrent = CurrentDb
Set rstPatterns = dbsCurrent.OpenRecordset(strTable)
' If IsNull(varFileContents) Then Exit Function
Dim objReg As VBScript_RegExp_55.RegExp
Set objReg = New VBScript_RegExp_55.RegExp
strFileContents = CStr(varFileContents)
With rstPatterns
rstPatterns.MoveFirst
'loop thru each row
Do While Not rstPatterns.EOF
If rstPatterns!Trigger = "Yes" Then
'loop thru each col
objReg.Pattern = rstPatterns!Pattern
blnMatchCase = rstPatterns!Case
' Set case insensitivity
objReg.IgnoreCase = Not (blnMatchCase)
objReg.Global = True
strReplace = Replace(rstPatterns!Replace, "|", "")
‘ERROR OCCURS BELOW
strFileContents = objReg.Replace(strFileContents,
strReplace)
strReplace = ""
End If
.MoveNext
Loop
End With
dbsCurrent.Close
Set objReg = Nothing
Set rstPatterns = Nothing
Set dbs = Nothing
End Function