Dynamically loop through all properties of a class module

G

Guest

Hello,

I've found that you can enumerate all modules of an access db using the
AllModules collection, but what if I want to enumerate all properties of a
particular module?

In particular, I'm using the Web Service References add-in, and want to use
a flexible loop to match up the fields of the complex structures used to pass
data back and forth without having to hard-code them.

Any ideas?

Thanks in advance!

--J
 
S

strive4peace

Hi J,

not exactly what you asked for, but hopefully you can see the logic and
pull what you need


'~~~~~~~~~~~~~
Sub DocModules( _
ByVal pPath As String _
)

'crystal
'strive4peace2006 at yahoo dot com

'CALLS
'DocProcs

'TABLES
'DocDbs
'DocMods
'DocModProcs

'REPORT
'rpt_DocProcs

'---------- Initialize
On Error GoTo Proc_Err

'---------- Dimension Variables
Dim accApp As Access.Application _
, dbCur As Database _
, rDb As DAO.Recordset _
, rMod As DAO.Recordset _
, rProc As DAO.Recordset

Dim mStartTime As Date _
, mMsg As String

Dim mdl As Module _
, iMod As Integer _
, iProc As Integer _
, S As String _
, mNumDbs As Integer _
, mNumMods As Integer _
, mNumProcs As Integer

Dim mFilename As String _
, mFileSpec As String _
, iNumFiles As Integer

Dim arrProcNames() As String _
, ModID_first As Long

Dim mDbID As Long _
, mModID As Long

'---------- Assign Variables
mStartTime = Now

mNumMods = 0
mNumProcs = 0

If Len(Trim(Nz(pPath, ""))) = 0 Then
pPath = CurrentProject.Path
End If

If Right(pPath, 1) <> "\" Then pPath = pPath & "\"

Set accApp = CreateObject("Access.Application")


Set dbCur = CurrentDb

Set rDb = dbCur.OpenRecordset("docDbs", dbOpenDynaset)
Set rMod = dbCur.OpenRecordset("docMods", dbOpenDynaset)
Set rProc = dbCur.OpenRecordset("docModProcs", dbOpenDynaset)

mFileSpec = pPath & "*.mdb"

mFilename = Dir(mFileSpec)

Do While Not Len(Trim(Nz(mFilename, ""))) = 0


accApp.OpenCurrentDatabase _
(pPath & mFilename)

rDb.AddNew
rDb!dbname = mFilename
rDb!dbpath = pPath
mDbID = rDb!DbID
rDb.Update

mNumDbs = mNumDbs + 1

ModID_first = 0


For iMod = 0 To accApp.Modules.Count - 1

With accApp.Modules(iMod)

Debug.Print "-- " & .Name
rMod.AddNew
rMod!DbID = mDbID
rMod!ModName = .Name
rMod!NumLines = .CountOfLines
rMod!NumLinesDecl = .CountOfDeclarationLines
mModID = rMod!ModID
rMod.Update

mNumMods = mNumMods + 1

If Left(.Name, 5) = "form_" Or Left(.Name, 7) = "report_" Then
GoTo nextModule
End If

MsgBox .Name, , ".Name"

DocProcs accApp, .Name, arrProcNames, False

End With

If ModID_first = 0 Then ModID_first = mModID

With accApp.Modules(iMod)
For iProc = LBound(arrProcNames) To UBound(arrProcNames)
Debug.Print "* " & arrProcNames(iProc)

rProc.AddNew
rProc!ModID = mModID
rProc!StartLine = Nz(.ProcStartLine(arrProcNames(iProc),
0), 0)
rProc!BodyLine = Nz(.ProcBodyLine(arrProcNames(iProc),
0), 0)
rProc!CountLines =
Nz(.ProcCountLines(arrProcNames(iProc), 0), 0)
rProc!procName = arrProcNames(iProc)
rProc.Update

mNumProcs = mNumProcs + 1

Next iProc
End With

nextModule:
Next iMod

mFilename = Dir()
Loop

mMsg = mNumProcs & " Procedures" _
& vbCrLf & " in" _
& vbCrLf & mNumMods & " Modules" _
& vbCrLf & " in" _
& vbCrLf & mNumDbs & " Databases" _
& vbCrLf & vbCrLf

mMsg = mMsg & "Start Time: " & Format(mStartTime, "hh:nn:ss") & vbCrLf _
& "End Time: " & Format(Now(), "hh:nn:ss") & " --> " _
& " Elapsed Time: " & Format((Now() - mStartTime) * 24 * 60 *
60, "0.####") & " seconds"

MsgBox mMsg, , "Done documenting modules"

DoCmd.OpenReport "rpt_DocProcs", acViewPreview, , "ModID >=" &
ModID_first

Proc_Exit:
On Error Resume Next
'close and release object variables

Set mdl = Nothing

rProc.Close
Set rProc = Nothing

rMod.Close
Set rMod = Nothing

rDb.Close
Set rDb = Nothing

accApp.Quit
Set accApp = Nothing

Set dbCur = Nothing

Exit Sub

Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number & " DocModules"
'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit

End Sub
'~~~~~~~~~~~~~~~

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'changed from MS Help:
Public Function DocProcs( _
pApp As Access.Application _
, ByVal pModuleName As String _
, ByRef parrProcNames _
, Optional SayMsg = True _
)

On Error GoTo Proc_Err

Dim mdl As Module
Dim lngCount As Long
Dim lngCountDecl As Long
Dim lngI As Long
Dim strProcName As String
' Dim parrProcNames() As String
Dim intI As Integer
Dim strMsg As String
Dim lngR As Long

' Open specified Module object.
pApp.DoCmd.OpenModule pModuleName

' Return reference to Module object.
Set mdl = pApp.Modules(pModuleName)

' Count lines in module.
lngCount = mdl.CountOfLines

' Count lines in Declaration section in module.
lngCountDecl = mdl.CountOfDeclarationLines

' Determine name of first procedure.
strProcName = mdl.ProcOfLine(lngCountDecl + 1, lngR)

' Initialize counter variable.
intI = 0

' Redimension array.
ReDim Preserve parrProcNames(intI)

' Store name of first procedure in array.
parrProcNames(intI) = strProcName

' Determine procedure name for each line after declarations.
For lngI = lngCountDecl + 1 To lngCount
' Compare procedure name with ProcOfLine property value.
If strProcName <> mdl.ProcOfLine(lngI, lngR) Then
' Increment counter.
intI = intI + 1
strProcName = mdl.ProcOfLine(lngI, lngR)
ReDim Preserve parrProcNames(intI)
' Assign unique procedure names to array.
parrProcNames(intI) = strProcName
End If
Next lngI

strMsg = "Procedures in module '" & pModuleName & "': " & vbCrLf &
vbCrLf
For intI = 0 To UBound(parrProcNames)
strMsg = strMsg & parrProcNames(intI) & vbCrLf
Next intI

' Message box listing all procedures in module.
If SayMsg Then MsgBox strMsg

Proc_Exit:
On Error Resume Next
Exit Function

Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number & " DocProcs"
'press F8 to step through code and debug
'remove next line after debugged
Stop: Resume
Resume Proc_Exit
End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Warm Regards,
Crystal
*
:) have an awesome day :)
*
MVP Access
Remote Programming and Training
strive4peace2006 at yahoo.com
*
 
P

Peter Yang [MSFT]

Hello,

You could add a breakpoint to the code that has module variable, and from
the Watch window you shall see all the properties of a module.

However, I'm not quite sure about the complex structures you referenced in
the module. If it's some structure defined in a module, it's not possible
that you could get directly from properties of a module, you may need to
get the codeline/string and process the code as you want:

208793 ACC2000: How to Programmatically Create, Search, Replace, and Modify
Code
http://support.microsoft.com/default.aspx?scid=kb;EN-US;208793

Please let me know if you have any comments or feedback. Thank you.

Best Regards,

Peter Yang
MCSE2000/2003, MCSA, MCDBA
Microsoft Online Partner Support

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=====================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Yang [MSFT]

Hi,

I'm still interested in this issue. If you have any comments or questions,
please feel free to let's know. We look forward to hearing from you.

Best Regards,

Peter Yang
MCSE2000/2003, MCSA, MCDBA
Microsoft Online Partner Support

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=====================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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