Count Module Lines

  • Thread starter Thread starter Ron Carr
  • Start date Start date
R

Ron Carr

I have a databse which reviews other databases and counts objects.
I would also like to count the lines of code in Modules and forms in the
other databases, but cannot make the connection to do so.
I am aware of the properties that yiend the module line count, it is a
matter of connecting to the module / Form.
Thanks for any help!
 
Ron said:
I have a databse which reviews other databases and counts objects.
I would also like to count the lines of code in Modules and forms in the
other databases, but cannot make the connection to do so.
I am aware of the properties that yiend the module line count, it is a
matter of connecting to the module / Form.

You have to open an object before you can use its
properties:

' Count lines of code in Modules
For Each doc In dbother.Containers("Modules").Documents
DoCmd.OpenModule doc.Name
With Modules(doc.Name)
Debug.Print .Name, .CountOfLines
End With
DoCmd.Close acModule, doc.Name, acSaveNo
Next doc
' Count lines of code in Forms
For Each doc In dbother.Containers("Forms").Documents
DoCmd.OpenForm doc.Name, acDesign
With Forms(doc.Name)
If .HasModule Then
Debug.Print .Name, .Module.CountOfLines
End If
End With
DoCmd.Close acForm, doc.Name, acSaveNo
Next doc
' Count lines of code in Reports
For Each doc In dbother.Containers("Reports").Documents
DoCmd.OpenReport doc.Name, acDesign
With Reports(doc.Name)
If .HasModule Then
Debug.Print .Name, .Module.CountOfLines
End If
End With
DoCmd.Close acReport, doc.Name, acSaveNo
Next doc
 
Thank you. I will give it a try.
Marshall Barton said:
You have to open an object before you can use its
properties:

' Count lines of code in Modules
For Each doc In dbother.Containers("Modules").Documents
DoCmd.OpenModule doc.Name
With Modules(doc.Name)
Debug.Print .Name, .CountOfLines
End With
DoCmd.Close acModule, doc.Name, acSaveNo
Next doc
' Count lines of code in Forms
For Each doc In dbother.Containers("Forms").Documents
DoCmd.OpenForm doc.Name, acDesign
With Forms(doc.Name)
If .HasModule Then
Debug.Print .Name, .Module.CountOfLines
End If
End With
DoCmd.Close acForm, doc.Name, acSaveNo
Next doc
' Count lines of code in Reports
For Each doc In dbother.Containers("Reports").Documents
DoCmd.OpenReport doc.Name, acDesign
With Reports(doc.Name)
If .HasModule Then
Debug.Print .Name, .Module.CountOfLines
End If
End With
DoCmd.Close acReport, doc.Name, acSaveNo
Next doc
 
I tried the following code, and got message "Can't find the module basAudit"
which is in fact the first module in the target database. Any idea what is
wrong?

Public Sub CountModuleLines2()
Dim doc As Document

Set dbs = DBEngine.Workspaces(0).OpenDatabase("D:\Access\Eastman\Eastman
Conversion.mdb", _
dbname = "Eastman Conversion.mdb", , True)

For Each doc In dbs.Containers("Modules").Documents
DoCmd.OpenModule doc.Name (Error occurs here)
With Modules(doc.Name)
vmodulelinecount = vmodulelinecount + .CountOfLines
End With
DoCmd.Close acModule, doc.Name, acSaveNo
Next doc
MsgBox "vmodulelinecount = " & vmodulelinecount
End Sub
 
The DoCmd is working in your current database, so it refers to the Modules
collection in that database, not in your Eastman Conversion.mdb. You're
going to have to use Automation to be able to read the modules in another
database. Try something like:

Sub ReadModule()
Dim objAccess As Access.Application
Dim docCurr As Document
Dim lngModuleLineCount As Long

Set objAccess = New Access.Application
objAccess.OpenCurrentDatabase "D:\Access\Eastman\Eastman Conversion.mdb"

For Each docCurr In
objAccess.DBEngine.Workspaces(0).Databases(0).Containers("Modules").Document
s
objAccess.DoCmd.OpenModule docCurr.Name
With objAccess.Modules(docCurr.Name)
lngModuleLineCount = lngModuleLineCount + .CountOfLines
End With
objAccess.DoCmd.Close acModule, docCurr.Name, acSaveNo
Next docCurr
MsgBox "lngModuleLineCount = " & lngModuleLineCount

objAccess.CloseCurrentDatabase
Set objAccess = Nothing

End Sub

To learn more about using Automation with Access, check out:

ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111



--
Doug Steele, Microsoft Access MVP



Ron Carr said:
I tried the following code, and got message "Can't find the module basAudit"
which is in fact the first module in the target database. Any idea what is
wrong?

Public Sub CountModuleLines2()
Dim doc As Document

Set dbs = DBEngine.Workspaces(0).OpenDatabase("D:\Access\Eastman\Eastman
Conversion.mdb", _
dbname = "Eastman Conversion.mdb", , True)

For Each doc In dbs.Containers("Modules").Documents
DoCmd.OpenModule doc.Name (Error occurs here)
With Modules(doc.Name)
vmodulelinecount = vmodulelinecount + .CountOfLines
End With
DoCmd.Close acModule, doc.Name, acSaveNo
Next doc
MsgBox "vmodulelinecount = " & vmodulelinecount
End Sub
 
That did it! Thanks. And thanks for thr references - I'll raed up some more.
Douglas J. Steele said:
The DoCmd is working in your current database, so it refers to the Modules
collection in that database, not in your Eastman Conversion.mdb. You're
going to have to use Automation to be able to read the modules in another
database. Try something like:

Sub ReadModule()
Dim objAccess As Access.Application
Dim docCurr As Document
Dim lngModuleLineCount As Long

Set objAccess = New Access.Application
objAccess.OpenCurrentDatabase "D:\Access\Eastman\Eastman Conversion.mdb"

For Each docCurr In
objAccess.DBEngine.Workspaces(0).Databases(0).Containers("Modules").Document
s
objAccess.DoCmd.OpenModule docCurr.Name
With objAccess.Modules(docCurr.Name)
lngModuleLineCount = lngModuleLineCount + .CountOfLines
End With
objAccess.DoCmd.Close acModule, docCurr.Name, acSaveNo
Next docCurr
MsgBox "lngModuleLineCount = " & lngModuleLineCount

objAccess.CloseCurrentDatabase
Set objAccess = Nothing

End Sub

To learn more about using Automation with Access, check out:

ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111
\
 
Douglas said:
The DoCmd is working in your current database, so it refers to the Modules
collection in that database, not in your Eastman Conversion.mdb. You're
going to have to use Automation to be able to read the modules in another
database. Try something like:

Sub ReadModule()
Dim objAccess As Access.Application
Dim docCurr As Document
Dim lngModuleLineCount As Long

Set objAccess = New Access.Application
objAccess.OpenCurrentDatabase "D:\Access\Eastman\Eastman Conversion.mdb"

For Each docCurr In
objAccess.DBEngine.Workspaces(0).Databases(0).Containers("Modules").Document
s
objAccess.DoCmd.OpenModule docCurr.Name
With objAccess.Modules(docCurr.Name)
lngModuleLineCount = lngModuleLineCount + .CountOfLines
End With
objAccess.DoCmd.Close acModule, docCurr.Name, acSaveNo
Next docCurr
MsgBox "lngModuleLineCount = " & lngModuleLineCount

objAccess.CloseCurrentDatabase
Set objAccess = Nothing

End Sub

To learn more about using Automation with Access, check out:

ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111

Aw sheesh, that'll teach me to take shortcuts when I check a
code snippet.

Thanks for bailing me out here Doug.
 
Marshall Barton said:
Douglas J. Steele wrote:

Aw sheesh, that'll teach me to take shortcuts when I check a
code snippet.

Thanks for bailing me out here Doug.

Hey, your code was interesting enough that I wanted to check out why it
wasn't working. <g>
 
Back
Top