Read text of form/report module code via VBA

D

Dale Fye

As mentioned in earlier posts, I'm writing my own Search and Replace
application.

I've figured out how to do almost everything I need to search the various
objects, but have not figured out how to refer to the code modules associated
with forms and reports. Anybody know the correct syntax for that?
 
J

Jack Leach

Have a look here:

http://www.cpearson.com/excel/vbe.aspx

this is written with excel in mind, but most of it is the same for access.



The reference required for Access is named

Microsoft Visual Basic for Applications Extensibility 5.3


a search for Access VBIDE will return some results.

Have fun :\

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
 
D

Dale Fye

Thanks, Jack.

That is precisely what I was looking for, well almost. But I think I can
get what I need from here.

Dale
 
D

Dirk Goldgar

Dale Fye said:
As mentioned in earlier posts, I'm writing my own Search and Replace
application.

I've figured out how to do almost everything I need to search the various
objects, but have not figured out how to refer to the code modules
associated
with forms and reports. Anybody know the correct syntax for that?


Dale -

Here's code for one approach, not using the VBA Extensibility library.
Forgive the newsreader-introduced line breaks; this is rough code I threw
together a while ago for my own use:

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

' Search the class modules of all forms that have them,
' looking for the specified string.

' Copyright (c) 2009, Dirk Goldgar and DataGnostics, LLC
' You may copy and use this code all you want, so long as
' you preserve the copyright and this notice, and don't
' sell it.

On Error GoTo Err_SearchFormModules

Dim db As DAO.Database
Dim doc As DAO.Document
Dim frm As Access.Form
Dim mdl As Access.Module

Dim lngFormCount As Long
Dim lngFoundCount As Long
Dim bOpenedForm As Boolean

Debug.Print "*** Searching form modules for '" & strSought & "' ..."

Set db = CurrentDb
For Each doc In db.Containers("Forms").Documents
If CurrentProject.AllForms(doc.Name).IsLoaded = False Then
DoCmd.OpenForm doc.Name, acDesign, WindowMode:=acHidden
bOpenedForm = True
Else
bOpenedForm = False
End If
Set frm = Forms(doc.Name)
With frm
If frm.HasModule Then
lngFormCount = lngFormCount + 1
lngFoundCount = lngFoundCount + SearchModule(frm.Module,
strSought, bWholeWord)
End If
If bOpenedForm Then
DoCmd.Close acForm, .Name, acSaveNo
End If

End With
Set frm = Nothing
Next doc

Exit_SearchFormModules:
Set doc = Nothing
Set db = Nothing
Debug.Print "*** Searched " & lngFormCount & _
" forms, found " & _
lngFoundCount & " occurrences."
Exit Sub

Err_SearchFormModules:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_SearchFormModules

End Sub


Sub SearchReportModules(strSought As String, Optional bWholeWord As Boolean)

' Search the class modules of all reports that have them,
' looking for the specified string.

' Copyright (c) 2009, Dirk Goldgar and DataGnostics, LLC
' You may copy and use this code all you want, so long as
' you preserve the copyright and this notice, and don't
' sell it.

On Error GoTo Err_SearchReportModules

Dim db As DAO.Database
Dim doc As DAO.Document
Dim rpt As Access.Report
Dim mdl As Access.Module

Dim lngReportCount As Long
Dim lngFoundCount As Long
Dim bOpenedReport As Boolean

Debug.Print "*** Searching report modules for '" & strSought & "' ..."

Set db = CurrentDb
For Each doc In db.Containers("Reports").Documents
If CurrentProject.AllReports(doc.Name).IsLoaded = False Then
DoCmd.OpenReport doc.Name, acDesign, WindowMode:=acHidden
bOpenedReport = True
Else
bOpenedReport = False
End If
Set rpt = Reports(doc.Name)
With rpt
If rpt.HasModule Then
lngReportCount = lngReportCount + 1
lngFoundCount = lngFoundCount + SearchModule(rpt.Module,
strSought, bWholeWord)
End If
If bOpenedReport Then
DoCmd.Close acReport, .Name, acSaveNo
End If

End With
Set rpt = Nothing
Next doc

Exit_SearchReportModules:
Set doc = Nothing
Set db = Nothing
Debug.Print "*** Searched " & lngReportCount & _
" reports, found " & _
lngFoundCount & " occurrences."
Exit Sub

Err_SearchReportModules:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_SearchReportModules

End Sub


Function SearchModule( _
mdl As Access.Module, _
strSought As String, _
Optional bWholeWord As Boolean) _
As Long

' Search module <mdl> for string <strSought>.
' Optional argument <bWholeWord> tells whether to report only
' occurrences of the string as a "whole word".

' Copyright (c) 2009, Dirk Goldgar and DataGnostics, LLC
' You may copy and use this code all you want, so long as
' you preserve the copyright and this notice, and don't
' sell it.

On Error GoTo Err_SearchModule

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

lngFoundCount = 0
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 " & _
IIf(mdl.Type = acStandardModule, "standard", "class") &
_
" module " & .Name & ", line " & lngStartLine & ", proc
'" & .ProcOfLine(lngStartLine, lngProcType) & "'"

lngStartLine = lngStartLine + 1

End If

Loop While bFound

End With

Exit_SearchModule:
SearchModule = lngFoundCount
Exit Function

Err_SearchModule:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_SearchModule

End Function
'------ end of code ------
 
D

Dale Fye

Thanks, Dirk.

That is going to save me a ton of time. My problem is that I am currently
working for the US Govt, and their IT policies will not allow me to use
shareware or freeware. I can write my own code, but cannot use someone
elses unless we pay for it, and have it tested/evaluated by our information
assurance people (takes 2-6 months, on the rare occassion that they agree to
do it at all). So, I'm writing my own Search/Replace tool, and this will
fill in the final puzzle piece.

Thanks again!

Dale
 
D

Dirk Goldgar

Dale Fye said:
Thanks, Dirk.

You're very welcome.
That is going to save me a ton of time. My problem is that I am currently
working for the US Govt, and their IT policies will not allow me to use
shareware or freeware. I can write my own code, but cannot use someone
elses unless we pay for it, and have it tested/evaluated by our
information assurance people (takes 2-6 months, on the rare occassion that
they agree to do it at all).

Argh! You have my deepest sympathies. On the other hand, it does force you
to be creative.
So, I'm writing my own Search/Replace tool, and this will fill in the
final puzzle piece.

Just in case you want it, here's the function that searches the Modules-tab
modules. It's not pretty when running, because it has to open each module
in order to search it.

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

' Search all standard and class modules for the specified string.

' Copyright (c) 2009, Dirk Goldgar and DataGnostics, LLC
' You may copy and use this code all you want, so long as
' you preserve the copyright and this notice, and don't
' sell it.

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
Dim bOpenedModule As Boolean

Debug.Print "*** Searching standard and class modules for '" & strSought
& "' ..."

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

For Each doc In cnt.Documents

lngSearchCount = lngSearchCount + 1

If CurrentProject.AllModules(doc.Name).IsLoaded = False Then
DoCmd.OpenModule doc.Name
bOpenedModule = True
Else
bOpenedModule = False
End If
Set mdl = Modules(doc.Name)

lngStartLine = 0
lngEndLine = 0

lngFoundCount = lngFoundCount + SearchModule(mdl, strSought,
bWholeWord)

If bOpenedModule = True Then
DoCmd.Close acModule, mdl.Name, acSaveNo
End If

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 ------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top