Number of Lines of Code

D

Daniel

Hello,

I have the following code to determine the number of lines of code in a db.

******
Dim db As Database
Dim Doc As Document
Dim mdl As Module
Dim lngCount As Long
Dim lngFormsCode As Long
Dim lngReportsCode As Long
Dim i As Integer
Dim strForm As String

Set db = CurrentDb()
' count module LOC
For Each Doc In db.Containers("Modules").Documents
DoCmd.OpenModule Doc.Name
Set mdl = Modules(Doc.Name)

lngCount = lngCount + mdl.CountOfLines
'Debug.Print mdl.CountOfLines, Doc.Name

Set mdl = Nothing
DoCmd.Close acModule, Doc.Name
Next

'Debug.Print lngCount & " lines of code in Modules."

' count Code in Forms
For i = 0 To db.Containers("Forms").Documents.Count - 1
strForm = db.Containers("Forms").Documents(i).Name
DoCmd.OpenForm strForm, acDesign
lngFormsCode = lngFormsCode + Forms(strForm).Module.CountOfLines
'Debug.Print strForm, Forms(strForm).Module.CountOfLines
DoCmd.Close acForm, strForm, acSaveNo
Next

'Debug.Print "forms code = " & lngFormsCode

' count LOC in Reports
For i = 0 To db.Containers("Reports").Documents.Count - 1
strForm = db.Containers("Reports").Documents(i).Name
DoCmd.OpenReport strForm, acViewDesign
Reports(strForm).Visible = False

lngReportsCode = lngReportsCode + Reports(strForm).Module.CountOfLines
'Debug.Print strForm, Reports(strForm).Module.CountOfLines
DoCmd.Close acReport, strForm, acSaveNo
Next

'Debug.Print "reports code = " & lngReportsCode

'Debug.Print "total = " & lngFormsCode + lngCount + lngReportsCode
'***
NumCode = lngFormsCode + lngCount + lngReportsCode
******

The question being, is there no way to do this without having to open a form
in design mode. Basically run it while a db is up (forms open) and return
the results to a form?

Thank you,

Daniel
 
D

Douglas J. Steele

No, I don't believe there's anyway to do it without the form being open in
Design mode.
 
M

Marshall Barton

Daniel said:
I have the following code to determine the number of lines of code in a db.

******
Dim db As Database
Dim Doc As Document
Dim mdl As Module
Dim lngCount As Long
Dim lngFormsCode As Long
Dim lngReportsCode As Long
Dim i As Integer
Dim strForm As String

Set db = CurrentDb()
' count module LOC
For Each Doc In db.Containers("Modules").Documents
DoCmd.OpenModule Doc.Name
Set mdl = Modules(Doc.Name)

lngCount = lngCount + mdl.CountOfLines
'Debug.Print mdl.CountOfLines, Doc.Name

Set mdl = Nothing
DoCmd.Close acModule, Doc.Name
Next

'Debug.Print lngCount & " lines of code in Modules."

' count Code in Forms
For i = 0 To db.Containers("Forms").Documents.Count - 1
strForm = db.Containers("Forms").Documents(i).Name
DoCmd.OpenForm strForm, acDesign
lngFormsCode = lngFormsCode + Forms(strForm).Module.CountOfLines
'Debug.Print strForm, Forms(strForm).Module.CountOfLines
DoCmd.Close acForm, strForm, acSaveNo
Next

'Debug.Print "forms code = " & lngFormsCode

' count LOC in Reports
For i = 0 To db.Containers("Reports").Documents.Count - 1
strForm = db.Containers("Reports").Documents(i).Name
DoCmd.OpenReport strForm, acViewDesign
Reports(strForm).Visible = False

lngReportsCode = lngReportsCode + Reports(strForm).Module.CountOfLines
'Debug.Print strForm, Reports(strForm).Module.CountOfLines
DoCmd.Close acReport, strForm, acSaveNo
Next

'Debug.Print "reports code = " & lngReportsCode

'Debug.Print "total = " & lngFormsCode + lngCount + lngReportsCode
'***
NumCode = lngFormsCode + lngCount + lngReportsCode
******

The question being, is there no way to do this without having to open a form
in design mode. Basically run it while a db is up (forms open) and return
the results to a form?


You need to check if an object was open before you started
meddling with it so you don't try to cahnge its view. You
probably don't want to vlose it if was already open.

Except for a form object being displayed as a subform in a
form that was already open, this seems to work (I did not
try it with an already open report with a subreport)
******************************************************************
Public Sub LinesInAllModules()
Dim cp As CodeProject
Dim ao As AccessObject
Dim bolWasOpen As Boolean
Dim lngReportsCode As Long, lngFormsCode As Long,
lngModulesCode As Long

Set cp = CurrentProject
' Count lines in Modules
For Each ao In cp.AllModules
bolWasOpen = cp.AllModules(ao.Name).IsLoaded
If Not bolWasOpen Then DoCmd.OpenModule ao.Name
lngModulesCode = lngModulesCode +
Modules(ao.Name).CountOfLines
If Not bolWasOpen Then DoCmd.Close acModule,
ao.Name, acSaveNo
Next ao
' Count lines of code in Forms
For Each ao In cp.AllForms
bolWasOpen = cp.AllForms(ao.Name).IsLoaded
If Not bolWasOpen Then DoCmd.OpenForm ao.Name,
acDesign, WindowMode:=acHidden
With Forms(ao.Name)
If .HasModule Then
lngFormsCode = lngFormsCode +
..Module.CountOfLines
End If
End With
If Not bolWasOpen Then DoCmd.Close acForm, ao.Name,
acSaveNo
Next ao
' Count lines of code in Reports
For Each ao In cp.AllReports
bolWasOpen = cp.AllReports(ao.Name).IsLoaded
If Not bolWasOpen Then DoCmd.OpenReport ao.Name,
acDesign, WindowMode:=acHidden
With Reports(ao.Name)
If .HasModule Then
lngReportsCode = lngReportsCode +
..Module.CountOfLines
End If
End With
If Not bolWasOpen Then DoCmd.Close acReport,
ao.Name, acSaveNo
Next ao

Debug.Print "Reports: " & lngReportsCode
Debug.Print "Forms: " & lngFormsCode
Debug.Print "Modules: " & lngModulesCode
Debug.Print "Total: " & (lngModulesCode + lngFormsCode +
lngReportsCode)

Set cp = Nothing
End Sub
******************************************************************

The problem with subforms (and subreports?) seems to be that
since they are not loaded, switching a displayed subform to
design view closes the main form. If you know exactly what
subform(s?) is being displayed, you can explicitly check for
its name and skip opening (and closing) it. E.g.
bolWasOpen = cp.AllModules(ao.Name).IsLoaded _
Or ao.Name = "sfmsomeform"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top