Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading the code in each module via collections

Newbie
 
Join Date: Feb 2007
Posts: 3
#1: Feb 5 '07
Is there a way to take a collections object of modules and read each
function and procedure and any declarations in it? I have 25 databases in Access that I need to programmatically go thru each module. Does anyone have
a method for doing this?

msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,886
#2: Feb 5 '07

re: Reading the code in each module via collections


Quote:

Originally Posted by Kganesan

Is there a way to take a collections object of modules and read each
function and procedure and any declarations in it? I have 25 databases in Access that I need to programmatically go thru each module. Does anyone have
a method for doing this?

The following query will return a list of all modules:

Expand|Select|Wrap|Line Numbers
  1. SELECT MsysObjects.Type, MsysObjects.Name FROM MsysObjects 
  2. WHERE ([Name] Not Like '~*') AND ([Name] Not Like 'MSys*') 
  3. AND [Type] = -32761
  4. ORDER BY Type, Name; 
  5.  
Save this query as qryModuleList.
Create new table to store the function list called tblFunctionList with the following fields:

FunctionID (PrimaryKey - Autonumber)
ModuleName
FunctionName

Expand|Select|Wrap|Line Numbers
  1. Function findFunctions()
  2. Dim db As DAO.Database
  3. Dim rs1 As DAO.Recordset
  4. Dim rs2 As DAO.Recordset
  5. Dim mdl As Module
  6. Dim i As Long
  7. Dim k As Long
  8. Dim strLine As String
  9. Dim intF As Integer
  10. Dim strFunc(5) As String
  11.  
  12.   strFunc(0) = "Private Sub"
  13.   strFunc(1) = "Public Sub"
  14.   strFunc(2) = "Sub"
  15.   strFunc(3) = "Private Function"
  16.   strFunc(4) = "Public Function"
  17.   strFunc(5) = "Function"
  18.  
  19.   Set db = CurrentDb
  20.   Set rs1 = db.OpenRecordset("qryModuleList")
  21.   Set rs2 = db.OpenRecordset("tblFunctionList")
  22.  
  23.   rs1.MoveFirst
  24.   Do until rs1.EOF
  25.  
  26.     ' Search the module for the function declaration line
  27.     i = 1
  28.     DoCmd.OpenModule (rs1![Name])
  29.     Set mdl = Modules(rs1![Name])
  30.     While i <= mdl.CountOfLines
  31.         strLine = mdl.Lines(i, 1)
  32.  
  33.         j = 0
  34.         Do While j <= UBound(strFunc)
  35.             intProc = InStr(MyLine, strFunc(j))
  36.             If (intF = 1) Then
  37.                 With rs2
  38.                     .AddNew
  39.                     !ModuleName = rs1![Name]
  40.                     !FunctionName = strLine
  41.                     .Update
  42.                 End With
  43.                 Exit Do
  44.             End If
  45.             j = j + 1
  46.         Loop
  47.  
  48.         i = i + 1
  49.     Wend
  50.     DoCmd.Close acModule, rs1![Name]
  51.     Set mdl = Nothing
  52.     rs1.MoveNext
  53.   Loop
  54.  
  55.   rs1.Close
  56.   rs2.Close
  57.   Set rs1=Nothing
  58.   Set rs2=Nothing
  59.   Set db=Nothing
  60.  
  61. End Function
  62.  
nico5038's Avatar
Moderator
 
Join Date: Nov 2006
Location: The Netherlands
Posts: 2,232
#3: Feb 5 '07

re: Reading the code in each module via collections


When you're allowed to spend some $'s I would check the Access tools at www.fmsinc.com.
The other solution would require VBA code to open the document collection of the containers and to process the code. Still the parsing of the text will be nasty...

Nic;o)
Reply