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
-
Mozilla quietly tests Perplexity AI as a New Firefox Search Option
by
Alex5723
52 minutes ago -
LibreOffice 25.8. No Windows 7, 8/8.1, x86
by
Alex5723
3 hours, 55 minutes ago -
Perplexity Pro free for 12 mos for Samsung Galaxy phones
by
Patricia Grace
19 hours, 51 minutes ago -
June KB5060842 update broke DHCP server service
by
Alex5723
18 hours, 23 minutes ago -
AMD Ryzen™ Chipset Driver Release Notes 7.06.02.123
by
Alex5723
22 hours, 25 minutes ago -
Excessive security alerts
by
WSSebastian42
49 minutes ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
1 day, 7 hours ago -
Ben’s excellent adventure with Linux
by
Ben Myers
1 hour, 12 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
19 hours, 2 minutes ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
1 hour, 35 minutes ago -
OS news from WWDC 2025
by
Will Fastie
5 hours, 10 minutes ago -
Need help with graphics…
by
WSBatBytes
3 hours, 12 minutes ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
1 day, 23 hours ago -
Totally remove or disable BitLocker
by
CWBillow
22 hours, 29 minutes ago -
Windows 10 gets 6 years of ESU?
by
n0ads
1 day, 1 hour ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
2 days, 10 hours ago -
Search Forums only bring up my posts?
by
Deo
4 hours, 34 minutes ago -
Windows Spotlight broken on Enterprise and Pro for Workstations?
by
steeviebops
2 days, 21 hours ago -
Denmark wants to dump Microsoft for Linux + LibreOffice
by
Alex5723
2 days, 14 hours ago -
How to get Microsoft Defender to honor Group Policy Setting
by
Ralph
2 days, 22 hours ago -
Apple : Paragon’s iOS Mercenary Spyware Finds Journalists Target
by
Alex5723
3 days, 8 hours ago -
Music : The Rose Room – It’s Been A Long, Long Time album
by
Alex5723
3 days, 9 hours ago -
Disengage Bitlocker
by
CWBillow
2 days, 23 hours ago -
Mac Mini M2 Service Program for No Power Issue
by
Alex5723
3 days, 11 hours ago -
New Win 11 Pro Geekom Setup questions
by
Deo
4 hours, 38 minutes ago -
Windows 11 Insider Preview build 26200.5651 released to DEV
by
joep517
3 days, 18 hours ago -
Windows 11 Insider Preview build 26120.4441 (24H2) released to BETA
by
joep517
3 days, 18 hours ago -
iOS 26,, MacOS 26 : Create your own AI chatbot
by
Alex5723
3 days, 22 hours ago -
New PC transfer program recommendations?
by
DaveBoston
2 days, 3 hours ago -
Windows 11 Insider Preview Build 22631.5545 (23H2) released to Release Preview
by
joep517
4 days, 2 hours ago
Recent blog posts
- Ben’s excellent adventure with Linux
- Seconds are back in Windows 10!
- WebBrowserPassView — Take inventory of your stored passwords
- OS news from WWDC 2025
- Best tools for upgrading a Windows 10 to an 11
- Master patch listing for June 10, 2025
- 24H2 may not be offered June updates
- June 2025 updates are out
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.