I want to write a routine to read all VBA modules in a database, whether it is freestanding or attached to a form (or report, for that matter). What approach can I take? I know how to read the collections, the tabledefs and querydefs and so on, but the modules collection does not appear to carry the actual module contents, only some high-level information. What am I missing?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
VBA to Read Modules (A2K)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » VBA to Read Modules (A2K)
- This topic has 7 replies, 4 voices, and was last updated 21 years, 11 months ago.
AuthorTopicWSDaleNapier
AskWoody LoungerJuly 18, 2003 at 2:14 pm #390656Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody Lounger -
WSDaleNapier
AskWoody Lounger -
WSDaleNapier
AskWoody LoungerJuly 18, 2003 at 3:32 pm #694791OK, got it. Now, more fun: reading the module with DoCmd.OpenModule assumes we want to open a module in the current database. Is there a way to open/read a module in another database without leaving the one we are in? I don’t need to change it necessarily (although that would be handy), but I do need to look at it from afar.
-
WScharlotte
AskWoody Lounger -
WSMarkD
AskWoody LoungerJuly 19, 2003 at 1:24 pm #694947(Edited by MarkD on 19-Jul-03 09:24. Corrected bug in code example.)
As recommended, you can use OpenCurrentDatabase method to open another (invisible) instance of Access, then open objects in another database. Revised sample sub:
Public Sub PrintModuleTextOther(ByVal strDbPath As String, _
ByVal strObjName As String, _
ByVal intObjType As AcObjectType)
On Error GoTo Err_Handler‘ strDbPath = full path to other db file to open – ex:
” C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb
‘ strObjName = name of object to open (form, report, or module)
‘ intObjType = type of object – AcObjectType enum:
” Const acForm = 2; Const acReport = 3; Const acModule = 5Dim app As Access.Application
Dim mdl As Access.Module
Dim n As Long
Dim strObjType As String
Dim strModType As String
Dim strMsg As String
Dim bFound As BooleanSet app = New Access.Application
app.Visible = False
app.OpenCurrentDatabase strDbPath, TrueSelect Case intObjType
Case 2 ‘ Form
app.DoCmd.OpenForm strObjName, acDesign
If app.Forms(strObjName).HasModule = True Then
Set mdl = app.Forms(strObjName).Module
strObjType = “Form”
bFound = True
Else
strMsg = “The selected form (” & strObjName & “) does not have a code module.”
MsgBox strMsg, vbExclamation, “NO MODULE”
bFound = False
End IfCase 3 ‘ Report
app.DoCmd.OpenReport strObjName, acViewDesign
If app.Reports(strObjName).HasModule = True Then
Set mdl = app.Reports(strObjName).Module
strObjType = “Report”
bFound = True
Else
strMsg = “The selected report (” & strObjName & “) does not have a code module.”
MsgBox strMsg, vbExclamation, “NO MODULE”
bFound = False
End IfCase 5 ‘ Module
app.DoCmd.OpenModule strObjName
Set mdl = app.Modules(strObjName)
strObjType = “Module”
bFound = TrueCase Else
MsgBox “Invalid object type specified.”, vbExclamation, “INVALID OBJECT”
bFound = False
End SelectIf bFound = True Then
Select Case mdl.Type
‘ AcModuleType constants:
Case 0
strModType = “Standard Module” ‘ acStandardModule
Case 1
strModType = “Class Module” ‘ acClassModule
End Selectn = mdl.CountOfLines
Debug.Print “Object Name: ” & strObjName & vbCrLf & _
“Object Type: ” & strObjType & vbCrLf & _
“Module Type: ” & strModType & vbCrLf & _
“Module text:” & vbCrLf & mdl.Lines(1, n)
End Ifapp.DoCmd.Close intObjType, strObjName
app.CloseCurrentDatabase
app.QuitExit_Sub:
Set app = Nothing
Set mdl = Nothing
Exit Sub
Err_Handler:
Select Case Err.Number
Case 7866 ‘Database not found or opened exclusively by other user
strMsg = “The database specified does not exist, ” & _
“or is opened exclusively by another user.”
MsgBox strMsg, vbExclamation, “CANNOT OPEN DATABASE”
Resume Exit_Sub
Case 2102, 2103, 2516 ‘Form, Report, Module not found
strMsg = “The object name and type specified does not exist.”
MsgBox strMsg, vbExclamation, “CANNOT OPEN OBJECT”
Resume Exit_Sub
Case Else
strMsg = “Error No ” & Err.Number & “: ” & Err.Description
MsgBox strMsg, vbExclamation, “PRINT MODULE TEXT ERROR”
Resume Exit_Sub
End SelectEnd Sub
Note modified previous example to test if form or report has code module (HasModule property), and to indicate whether module is a standard or class module in case that is significant.. I tested this with Northwind.mdb. Example:
PrintModuleTextOther “C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb”,”Orders”,acForm
The code module for Northwind “Orders” form was “printed” to debug window. Note if you open code module the VBE window will be briefly visible because the VB Editor opens in its own window. There are probably more direct ways to access a code module but these likely are not readily available to end-users.
HTH
-
WSMarkD
AskWoody LoungerJuly 21, 2003 at 3:09 pm #695414Looked at this one last time, here is simplified method that does not require opening object, thus eliminating annoying screen “flash” when a module is opened:
Public Sub PrintModuleTextRev(ByVal strDbPath As String, _
ByVal strObjName As String, _
ByVal intObjType As AcObjectType)
On Error GoTo Err_HandlerDim app As Access.Application
Dim n As Long
Dim strObjType As String
Dim strModType As String
Dim strMsg As StringSet app = New Access.Application
app.Visible = False
app.OpenCurrentDatabase strDbPath, TrueSelect Case intObjType
Case 2 ‘ Form
strObjType = “Form”
strObjName = “Form_” & strObjNameCase 3 ‘ Report
strObjType = “Report”
strObjName = “Report_” & strObjNameCase 5 ‘ Module
strObjType = “Module”
End SelectSelect Case app.VBE.ActiveVBProject.VBComponents.Item(strObjName).Type
Case 1 ‘ vbext_ct_StdModule
strModType = “Standard Module”
Case 2 ‘ vbext_ct_ClassModule
strModType = “Class Module”
Case Else
‘ Form and Report = VBComponent Type 100 – not listed
strModType = “MS Access Class Object”
End Selectn = app.VBE.ActiveVBProject.VBComponents.Item(strObjName).CodeModule.CountOfLines
Debug.Print “Object Name: ” & strObjName & vbCrLf & _
“Object Type: ” & strObjType & vbCrLf & _
“Module Type: ” & strModType & vbCrLf & _
“Module text:” & vbCrLf & app.VBE.ActiveVBProject.VBComponents.Item(strObjName).CodeModule.Lines(1, n)app.CloseCurrentDatabase
app.QuitExit_Sub:
Set app = Nothing
Exit Sub
Err_Handler:
Select Case Err.Number
Case 9 ‘Subscript Out of Range (ie, VBComponent not found for specified object name)
strMsg = “Code module for object name specified was not found in database specified.”
MsgBox strMsg, vbExclamation, “OBJECT NOT FOUND”
Case 7866 ‘Database not found or opened exclusively by other user
strMsg = “The database specified does not exist, ” & _
“or is opened exclusively by another user.”
MsgBox strMsg, vbExclamation, “CANNOT OPEN DATABASE”
Case Else
strMsg = “Error No ” & Err.Number & “: ” & Err.Description
MsgBox strMsg, vbExclamation, “PRINT MODULE TEXT ERROR”
End Select
Resume Exit_Sub
End SubFor further info, set a reference to VB Extensibility library (VBIDE) (VB6EXT.OLB) and examine the objects, properties & methods available.
-
-
-
-
WSMarkD
AskWoody LoungerJuly 18, 2003 at 3:41 pm #694794Not sure what you need this for, but here is sample sub that will print the contents of any form module, report module, or standard or class module to the Debug window:
Public Sub PrintModuleText(ByVal strObjName As String, _
ByVal intObjType As AcObjectType)‘ strObjName = name of object to open (form, report, module)
‘ intObjType = type of object – valid obj types:
‘ Const acForm = 2
‘ Const acReport = 3
‘ Const acModule = 5
‘ AcModuleType constants:
‘ Const acStandardModule = 0
‘ Const acClassModule = 1Dim mdl As Access.Module
Dim n As Long
Dim strObjType As StringSelect Case intObjType
Case 2 ‘ Form
DoCmd.OpenForm strObjName, acDesign
Set mdl = Forms(strObjName).Module
strObjType = “Form”Case 3 ‘ Report
DoCmd.OpenReport strObjName, acViewDesign
Set mdl = Reports(strObjName).Module
strObjType = “Report”Case 5 ‘ Module
DoCmd.OpenModule strObjName
Set mdl = Modules(strObjName)
strObjType = “Module”Case Else
MsgBox “Invalid object type specified.”, vbExclamation, “INVALID OBJECT”
Set mdl = Nothing
Exit Sub
End Selectn = mdl.CountOfLines
Debug.Print “Object Name: ” & strObjName & vbCrLf & _
“Object Type: ” & strObjType & vbCrLf & _
“Module text:” & vbCrLf & _
mdl.Lines(1, n)Select Case intObjType
Case 2 ‘ Form
DoCmd.Close acForm, strObjName
Case 3 ‘ Report
DoCmd.Close acReport, strObjName
Case 5 ‘ Module
DoCmd.Close acModule, strObjName
End SelectSet mdl = Nothing
End Sub
Note that the object must be open to be able to “read” the code module. You may be able to adapt this for your purposes, note that error-handling should be added. (If you open form or report w/o code module, it’ll open form in design view and ask you if you want to save changes….).
HTH
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
3 hours ago -
Update from WinPro 10 v. 1511 on T460p?
by
CatoRenasci
18 minutes ago -
System Restore and Updates Paused
by
veteran
5 hours, 30 minutes ago -
Windows 10/11 clock app
by
Kathy Stevens
1 hour, 46 minutes ago -
Turn off right-click draw
by
Charles Billow
8 hours, 44 minutes ago -
Introducing ChromeOS M137 to The Stable Channel
by
Alex5723
12 hours, 15 minutes ago -
Brian Wilson (The Beach Boys) R.I.P
by
Alex5723
1 hour, 19 minutes ago -
Master patch listing for June 10, 2025
by
Susan Bradley
13 hours, 52 minutes ago -
Suggestions for New All in One Printer and a Photo Printer Windows 10
by
Win7and10
5 hours, 54 minutes ago -
Purchasing New Printer. Uninstall old Printer Software First?
by
Win7and10
19 hours, 54 minutes ago -
KB5060842 Issue (Minor)
by
AC641
1 day ago -
EchoLeak : Zero Click M365 Copilot leak sensitive information
by
Alex5723
1 day, 2 hours ago -
24H2 may not be offered June updates
by
Susan Bradley
1 hour, 37 minutes ago -
Acronis : Tracking Chaos RAT’s evolution (Windows, Linux)
by
Alex5723
1 day, 15 hours ago -
June 2025 updates are out
by
Susan Bradley
3 hours, 51 minutes ago -
Mozilla shutting Deep Fake Detector
by
Alex5723
2 days, 6 hours ago -
Windows-Maintenance-Tool (.bat)
by
Alex5723
1 day, 15 hours ago -
Windows 11 Insider Preview build 26200.5641 released to DEV
by
joep517
2 days, 8 hours ago -
Windows 11 Insider Preview build 26120.4250 (24H2) released to BETA
by
joep517
2 days, 8 hours ago -
Install Office 365 Outlook classic on new Win11 machine
by
WSrcull999
2 days, 8 hours ago -
win 10 to win 11 with cpu/mb replacement
by
aquatarkus
2 days ago -
re-install Windows Security
by
CWBillow
2 days, 12 hours ago -
WWDC 2025 Recap: All of Apple’s NEW Features in 10 Minutes!
by
Alex5723
2 days, 15 hours ago -
macOS Tahoe 26
by
Alex5723
2 days, 10 hours ago -
Migrating from win10 to win11, instructions coming?
by
astro46
42 minutes ago -
Device Eligibility for Apple 2026 Operating Systems due this Fall
by
PKCano
2 days ago -
Recommended watching : Mountainhead movie
by
Alex5723
2 days, 1 hour ago -
End of support for Windows 10
by
Old enough to know better
7 minutes ago -
What goes on inside an LLM
by
Michael Covington
1 hour, 4 minutes ago -
The risk of remote access
by
Susan Bradley
7 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.