Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Procedure Documenter
Brian Crawford 
    
2 years ago
I thought you and your users might like this code I created using ChatGPT.  It is a Sub that creates a table that contains a list of every Function and Sub in the database.  It is very useful when you have hundreds of Function/Subs and can't remember the name, what the options are., or even which module it is in.  This Sub writes to a table that you specify, which can be later exported to Excel or printed.

Feel free to do what ever you want with this, and share with anyone.

DetailsPublic Sub DocAllPublicModulesToTable(tableName As String, Optional includeForms As Boolean = False)
' REQUIRES REFERENCE: Microsoft Visual Basic for Applications Extensibility 5.3
' This Sub creates a table (tableName) that summarizes all the Functions and Subs in the Access
' Workbook.  The optional parameter includeForms (False by default) includes/excludes documenting code
' contained within the Access Forms modules
'  The summary table contains the following fields:
'       ModuleType: Class, Code, or Form
'       ModuleName: The name of module where the Function or Sub was found
'       Scope: Public or Private
'       CodeType: Function or Sub
'       Name: The name of the Function or Sub
'       ReturnType: If a Function, the data type returned by the Function
'       Declaration: The full declaration of the Function or Sub showing all parameters, data types, options/defaults
'
    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim rst As DAO.Recordset
    Dim vbc As VBComponent
    Dim cm As CodeModule
    Dim i As Long, lineCount As Long
    Dim declaration As String, fullDeclaration As String
    Dim moduleType As String, scope As String, codeType As String, name As String, returnType As String
    Dim pos As Long, endPos As Long
    
    ' Set database reference
    Set db = CurrentDb
    
    ' Delete table if it exists
    On Error Resume Next
    db.TableDefs.Delete tableName
    On Error GoTo 0
    
    ' Create new table
    Set tdf = db.CreateTableDef(tableName)
    With tdf
        .fields.Append .CreateField("ModuleType", dbText)
        .fields.Append .CreateField("ModuleName", dbText)
        .fields.Append .CreateField("Scope", dbText)
        .fields.Append .CreateField("CodeType", dbText)
        .fields.Append .CreateField("Name", dbText)
        .fields.Append .CreateField("ReturnType", dbText)
        .fields.Append .CreateField("Declaration", dbMemo)
    End With
    db.TableDefs.Append tdf
    
    ' Open recordset
    Set rst = db.OpenRecordset(tableName, dbOpenDynaset)
    
    ' Loop through all modules
    For Each vbc In Application.VBE.VBProjects(1).VBComponents
        ' Determine the type of module
        Select Case vbc.Type
            Case vbext_ct_StdModule
                moduleType = "Code"
            Case vbext_ct_ClassModule
                moduleType = "Class"
            Case vbext_ct_Document
                If includeForms Then
                    moduleType = "Form"
                Else
                    GoTo SkipModule
                End If
            Case Else
                GoTo SkipModule
        End Select
        
        Set cm = vbc.CodeModule
        lineCount = cm.CountOfLines
        
        ' Loop through each line of the module
        i = 1
        Do While i <= lineCount
            declaration = cm.Lines(i, 1)
            
            If Left(declaration, 7) = "Public " Or Left(declaration, 8) = "Private " Then
                ' Collect full declaration if it spans multiple lines
                fullDeclaration = declaration
                Do While Right(fullDeclaration, 1) = "_"
                    i = i + 1
                    fullDeclaration = Left(fullDeclaration, Len(fullDeclaration) - 1) & " " & Trim(cm.Lines(i, 1))
                Loop
                
                ' Remove comments
                pos = InStr(fullDeclaration, "'")
                If pos > 0 Then
                    fullDeclaration = Left(fullDeclaration, pos - 1)
                End If
                fullDeclaration = Trim(fullDeclaration)
                
                ' Determine if it's a Sub or Function
                If InStr(fullDeclaration, " Sub ") > 0 Then
                    codeType = "Sub"
                ElseIf InStr(fullDeclaration, " Function ") > 0 Then
                    codeType = "Function"
                Else
                    ' Skip lines that are not declarations
                    GoTo NextLine
                End If
                
                ' Determine scope
                If Left(fullDeclaration, 7) = "Public " Then
                    scope = "Public"
                Else
                    scope = "Private"
                End If
                
                ' Extract name and return type
                pos = InStr(fullDeclaration, codeType)
                If pos > 0 Then
                    name = Trim(Mid(fullDeclaration, pos + Len(codeType) + 1))
                    pos = InStr(name, "(")
                    If pos > 0 Then
                        name = Left(name, pos - 1)
                    End If
                    
                    If codeType = "Function" Then
                        pos = InStrRev(fullDeclaration, " As ")
                        If pos > 0 Then
                            ' Check if the As clause is part of the function signature or a parameter
                            endPos = InStrRev(fullDeclaration, ")")
                            If endPos > 0 And pos > endPos Then
                                returnType = Trim(Mid(fullDeclaration, pos + 4))
                            Else
                                returnType = "Variant"
                            End If
                        Else
                            returnType = "Variant"
                        End If
                    Else
                        returnType = ""
                    End If
                End If
                
                ' Add record to table
                rst.AddNew
                rst!moduleType = moduleType
                rst!ModuleName = vbc.name
                rst!scope = scope
                rst!codeType = codeType
                rst!name = name
                If Len(returnType) > 0 Then
                    rst!returnType = returnType
                Else
                    rst!returnType = Null
                End If
                rst!declaration = fullDeclaration
                rst.Update
            End If
            
NextLine:
            i = i + 1
        Loop
SkipModule:
    Next vbc
    
    ' Clean up
    rst.Close
    Set rst = Nothing
    Set tdf = Nothing
    Set db = Nothing
End Sub
Richard Rost  @Reply  
          
2 years ago
Very cool. Thanks for sharing!
Ronald de Boer  @Reply  
      
2 years ago
Yes please.  How do I get it.
Sami Shamma  @Reply  
             
2 years ago
click on the "Details" at the bottom of the original post.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 6:53:31 PM. PLT: 0s