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 
VBA for Counting lines of code
Brian Crawford 
    
3 years ago
Some useful code... I wanted to count the lines of code in my Access database, and looked around for a tool. I found something from 2007 that no longer worked, so I asked ChatGPT to create one for me.  I also asked to include a parameter to choose between varying levels of verboseness.  It consists of a function that may be called in the immediate window and a related helper function.

     Usage:  CountLinesInAllModules
          or      CountLinesInAllModules 3 ' for more verbose detail

I added the following code as its own module to be easier to be portable between Access projects.


'---------------------------------- (begin code module)-----
Option Compare Database
Option Explicit

Public Function CountLinesInAllModules(Optional iVerboseLevel As Integer = 3) As Long
' Purpose:   Count the number of lines of code in your database.
' Argument:  iVerboseLevel: This number is a bit field, indicating what should print to the Immediate Window:
'               0 displays nothing
'               1 displays a summary for the module type (form, report, stand-alone.)
'               2 lists the lines in each module
'               3 displays the summary and the list of modules.

    Dim obj As Object
    Dim modul As Module
    Dim lineCount As Long
    Dim objType As String

    ' Count lines in standalone modules
    objType = "stand-alone module(s)"
    For Each obj In CurrentProject.AllModules
        Set modul = Application.Modules(obj.Name)
        lineCount = lineCount + CountLinesInModule(modul, obj.Name, iVerboseLevel, objType)
    Next obj

    ' Count lines in forms
    objType = "module(s) behind forms"
    For Each obj In CurrentProject.AllForms
        DoCmd.OpenForm obj.Name, acDesign, WindowMode:=acHidden
        If Forms(obj.Name).HasModule Then
            On Error Resume Next
            Set modul = Application.Modules("Form_" & obj.Name)
            If Err.Number = 0 Then
                lineCount = lineCount + CountLinesInModule(modul, "Form_" & obj.Name, iVerboseLevel, objType)
            End If
            On Error GoTo 0
        End If
        DoCmd.Close acForm, obj.Name, acSaveNo
    Next obj

    ' Count lines in reports
    objType = "module(s) behind reports"
    For Each obj In CurrentProject.AllReports
        DoCmd.OpenReport obj.Name, acDesign, WindowMode:=acHidden
        If Reports(obj.Name).HasModule Then
            On Error Resume Next
            Set modul = Application.Modules("Report_" & obj.Name)
            If Err.Number = 0 Then
                lineCount = lineCount + CountLinesInModule(modul, "Report_" & obj.Name, iVerboseLevel, objType)
            End If
            On Error GoTo 0
        End If
        DoCmd.Close acReport, obj.Name, acSaveNo
    Next obj

    ' Print total count and return
    If (iVerboseLevel And 1) <> 0 Then
        Debug.Print "Total: " & lineCount & " line(s)"
    End If
    CountLinesInAllModules = lineCount
End Function


Private Function CountLinesInModule(modul As Module, objName As String, iVerboseLevel As Integer, objType As String) As Long
    Dim lineCount As Long
    lineCount = modul.CountOfLines
    If (iVerboseLevel And 2) <> 0 Then
        Debug.Print objName & ": " & lineCount & " line(s)"
    End If
    If (iVerboseLevel And 1) <> 0 Then
        Debug.Print lineCount & " line(s) in " & objType
    End If
    CountLinesInModule = lineCount
End Function
Kevin Yip  @Reply  
     
3 years ago
The code won't work because in order to count lines of code, the module must be opened first.  Opening a module requires the line DoCmd.OpenModule modulename, which is missing from the code above.  ChatGPT is actually in good form here, because I've seen it make a lot more mistakes, or simply write outright nonsensical code.
Alex Hedley  @Reply  
           
3 years ago
If you are backing up using vcs you could write some simple code to count lines in the exported files

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: 5/6/2026 3:52:31 PM. PLT: 1s