search for string in module

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Could anyone point me in the right direction on an automated way to search
through each module in turn, looking for instances of a particular string -
(or tell me it cant be done).
Thanks
 
Wez.k said:
Could anyone point me in the right direction on an automated way to
search through each module in turn, looking for instances of a
particular string - (or tell me it cant be done).

What follows is a very quick, rough version of a sub to search through
all the standard and class modules -- but not form or report modules --
for a particular string, and display the result in the Immediate Window.

'----- start of code -----
Sub SearchModules( _
strSought As String, _
Optional bWholeWord As Boolean)

' Search all modules for the specified string.

On Error GoTo Err_SearchModules

Dim db As DAO.Database
Dim cnt As DAO.Container
Dim doc As DAO.Document
Dim mdl As Access.Module

Dim bFound As Boolean
Dim lngSearchCount As Long
Dim lngFoundCount As Long
Dim lngStartLine As Long
Dim lngEndLine As Long
Dim lngStartCol As Long
Dim lngEndCol As Long
Dim lngProcType As Long

Debug.Print "*** Beginning search ..."

Set db = CurrentDb
Set cnt = db.Containers("Modules")

For Each doc In cnt.Documents

lngSearchCount = lngSearchCount + 1

DoCmd.OpenModule doc.Name
Set mdl = Modules(doc.Name)

lngStartLine = 0
lngEndLine = 0

With mdl

Do
lngEndLine = 0
lngStartCol = 0
lngEndCol = 0

bFound = .Find(strSought, _
lngStartLine, _
lngStartCol, _
lngEndLine, _
lngEndCol, _
bWholeWord)

If bFound Then

lngFoundCount = lngFoundCount + 1

Debug.Print _
"Found in module " & .Name & _
", line " & lngStartLine & _
", proc " & _
.ProcOfLine(lngStartLine, lngProcType)

lngStartLine = lngStartLine + 1

End If

Loop While bFound

If .Name <> "basSearchModules" Then
DoCmd.Close acModule, .Name, acSaveNo
End If

End With

Set mdl = Nothing

Next doc

Exit_SearchModules:
Set cnt = Nothing
Set db = Nothing
Debug.Print "*** Searched " & lngSearchCount & _
" modules, found " & lngFoundCount & " occurrences."
Exit Sub

Err_SearchModules:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_SearchModules

End Sub
'----- end of code -----

Note that the name of the module from which this routine is running is
hard-coded as "basSearchModules". You should change that to the name of
the module in which you store this procedure, if it is not a form or
report module.
 
Thanks Dirk, that will get me started. Could you explain why this doesn't do
Form or Report modules?
 
Wez.k said:
Thanks Dirk, that will get me started. Could you explain why this
doesn't do Form or Report modules?

Because their modules -- and any given form or report may or may not
have one -- aren't stored as documents in the Modules document
container. For them you would use the Forms or Reports document
container (or, in A2K or later, the CurrentProject.AllForms or
AllReports collection) and would open each form or report in design
view, check the HasModule property to see if it has a module, and if it
does, use the Module property to get a reference to the Module object
that belongs to the form or report.
 
Back
Top