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?
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
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, 10 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
-
My Simple Word 2010 Macro Is Not Working
by
mbennett555
9 hours, 56 minutes ago -
Office gets current release
by
Susan Bradley
14 hours, 40 minutes ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
1 day, 7 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
15 hours, 43 minutes ago -
Stop the OneDrive defaults
by
CWBillow
1 day, 8 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
1 day, 17 hours ago -
X Suspends Encrypted DMs
by
Alex5723
1 day, 20 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
1 day, 20 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
1 day, 21 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
1 day, 21 hours ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
1 day, 9 hours ago -
Enabling Secureboot
by
ITguy
1 day, 16 hours ago -
Windows hosting exposes additional bugs
by
Susan Bradley
2 days, 5 hours ago -
No more rounded corners??
by
CWBillow
2 days, 1 hour ago -
Android 15 and IPV6
by
Win7and10
1 day, 15 hours ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
2 days, 17 hours ago -
T-Mobile’s T-Life App has a “Screen Recording Tool” Turned on
by
Alex5723
2 days, 20 hours ago -
Windows 11 Insider Preview Build 26100.4202 (24H2) released to Release Preview
by
joep517
2 days, 15 hours ago -
Windows Update orchestration platform to update all software
by
Alex5723
3 days, 3 hours ago -
May preview updates
by
Susan Bradley
2 days, 15 hours ago -
Microsoft releases KB5061977 Windows 11 24H2, Server 2025 emergency out of band
by
Alex5723
2 days, 6 hours ago -
Just got this pop-up page while browsing
by
Alex5723
2 days, 20 hours ago -
KB5058379 / KB 5061768 Failures
by
crown
2 days, 17 hours ago -
Windows 10 23H2 Good to Update to ?
by
jkitc
1 day, 19 hours ago -
At last – installation of 24H2
by
Botswana12
3 days, 19 hours ago -
MS-DEFCON 4: As good as it gets
by
Susan Bradley
17 hours, 12 minutes ago -
RyTuneX optimize Windows 10/11 tool
by
Alex5723
4 days, 7 hours ago -
Can I just update from Win11 22H2 to 23H2?
by
Dave Easley
2 days, 6 hours ago -
Limited account permission error related to Windows Update
by
gtd12345
4 days, 21 hours ago -
Another test post
by
gtd12345
4 days, 21 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.