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, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
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, 9 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
-
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
8 hours, 52 minutes ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
4 hours, 54 minutes ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
11 hours, 18 minutes ago -
Return of the brain dead FF sidebar
by
EricB
1 hour, 55 minutes ago -
windows settings managed by your organization
by
WSDavidO61
6 hours ago -
Securing Laptop for Trustee Administrattor
by
PeachesP
6 hours, 13 minutes ago -
The local account tax
by
Susan Bradley
2 hours, 3 minutes ago -
Recall is back with KB5055627(OS Build 26100.3915) Preview
by
Alex5723
17 hours, 54 minutes ago -
Digital TV Antenna Recommendation
by
Win7and10
10 hours, 27 minutes ago -
Server 2019 Domain Controllers broken by updates
by
MP Support
1 day, 5 hours ago -
Google won’t remove 3rd party cookies in Chrome as promised
by
Alex5723
1 day, 7 hours ago -
Microsoft Manager Says macOS Is Better Than Windows 11
by
Alex5723
1 day, 10 hours ago -
Outlook (NEW) Getting really Pushy
by
RetiredGeek
13 hours, 4 minutes ago -
Steps to take before updating to 24H2
by
Susan Bradley
3 hours, 53 minutes ago -
Which Web browser is the most secure for 2025?
by
B. Livingston
17 hours, 30 minutes ago -
Replacing Skype
by
Peter Deegan
6 hours, 3 minutes ago -
FileOptimizer — Over 90 tools working together to squish your files
by
Deanna McElveen
1 day, 4 hours ago -
Excel Macro — ask for filename to be saved
by
nhsj
2 hours ago -
Trying to backup Win 10 computer to iCloud
by
SheltieMom
5 hours, 50 minutes ago -
Windows 11 Insider Preview build 26200.5570 released to DEV
by
joep517
3 days, 10 hours ago -
Windows 11 Insider Preview build 26120.3941 (24H2) released to BETA
by
joep517
3 days, 12 hours ago -
Windows 11 Insider Preview Build 22635.5305 (23H2) released to BETA
by
joep517
3 days, 12 hours ago -
No April cumulative update for Win 11 23H2?
by
Peobody
2 days ago -
AugLoop.All (TEST Augmentation Loop MSIT)
by
LarryK
3 days, 12 hours ago -
Boot Sequence for Dell Optiplex 7070 Tower
by
Serge Carniol
4 days, 3 hours ago -
OTT Upgrade Windows 11 to 24H2 on Unsupported Hardware
by
bbearren
4 days, 7 hours ago -
Inetpub can be tricked
by
Susan Bradley
2 days, 15 hours ago -
How merge Outlook 2016 .pst file w/into newly created Outlook 2024 install .pst?
by
Tex265
3 days, 1 hour ago -
FBI 2024 Internet Crime Report
by
Alex5723
4 days, 11 hours ago -
Perplexity CEO says its browser will track everything users do online
by
Alex5723
1 day, 20 hours 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.