Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Module Text in MS Access using VBA

Tags:

vba

ms-access

How do I do a search and replace of text within a module in Access from another module in access? I could not find this on Google.

FYI, I figured out how to delete a module programatically:

Call DoCmd.DeleteObject(acModule, modBase64)

like image 869
Greg Finzer Avatar asked Oct 15 '22 15:10

Greg Finzer


2 Answers

I assume you mean how to do this programatically (otherwise it's just ctrl-h). Unless this is being done in the context of a VBE Add-In, it is rarely (if ever) a good idea. Self modifying code is often flagged by AV software an although access will let you do it, it's not really robust enough to handle it, and can lead to corruption problems etc. In addition, if you go with self modifying code you are preventing yourself from ever being able to use an MDE or even a project password. In other words, you will never be able to protect your code. It might be better if you let us know what problem you are trying to solve with self modifying code and see if a more reliable solution could be found.

like image 90
Oorang Avatar answered Oct 18 '22 08:10

Oorang


After a lot of searching I found this code:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function to Search for a String in a Code Module. It will return True if it is found and
'False if it is not. It has an optional parameter (NewString) that will allow you to
'replace the found text with the NewString. If NewString is not included in the call
'to the function, the function will only find the string not replace it.
'
'Created by Joe Kendall 02/07/2003
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function SearchOrReplace(ByVal ModuleName As String, ByVal StringToFind As String, _
        Optional ByVal NewString, Optional ByVal FindWholeWord = False, _
        Optional ByVal MatchCase = False, Optional ByVal PatternSearch = False) As Boolean

    Dim mdl As Module
    Dim lSLine As Long
    Dim lELine As Long
    Dim lSCol As Long
    Dim lECol As Long
    Dim sLine As String
    Dim lLineLen As Long
    Dim lBefore As Long
    Dim lAfter As Long
    Dim sLeft As String
    Dim sRight As String
    Dim sNewLine As String

    Set mdl = Modules(ModuleName)

    If mdl.Find(StringToFind, lSLine, lSCol, lELine, lECol, FindWholeWord, _
            MatchCase, PatternSearch) = True Then
        If IsMissing(NewString) = False Then
            ' Store text of line containing string.
            sLine = mdl.Lines(lSLine, Abs(lELine - lSLine) + 1)
            ' Determine length of line.
            lLineLen = Len(sLine)
            ' Determine number of characters preceding search text.
            lBefore = lSCol - 1
            ' Determine number of characters following search text.
            lAfter = lLineLen - CInt(lECol - 1)
            ' Store characters to left of search text.
            sLeft = Left$(sLine, lBefore)
            ' Store characters to right of search text.
            sRight = Right$(sLine, lAfter)
            ' Construct string with replacement text.
            sNewLine = sLeft & NewString & sRight
            ' Replace original line.
            mdl.ReplaceLine lSLine, sNewLine
        End If
        SearchOrReplace = True
    Else
        SearchOrReplace = False
    End If

    Set mdl = Nothing
End Function
like image 27
Greg Finzer Avatar answered Oct 18 '22 09:10

Greg Finzer