[AccessD] Looking at lines of code in code

A.D.TEJPAL adtp at airtelbroadband.in
Sat Mar 10 12:27:09 CST 2007


Rocky,

    You should be able to obtain the required information directly from the modules collection.

    Sample sub-routine named P_GetMatchingCodeLines, as given below, will list the lines in various modules (along with names of respective modules) satisfying your criteria, in the immediate window. For directing the output to a table, Debug.Print statements in the subroutine can be substituted by suitable code.

    For your specific case, this procedure will be called via following statement:

    P_GetMatchingCodeLines "TranslateMsgbox"

    Important - You have to set reference to Microsoft Visual Basic For Applications Extensibility 5.3

Best wishes,
A.D.Tejpal
---------------

===================================
Public Sub P_GetMatchingCodeLines(ByVal _
                                            MatchString As String)
    ' This needs reference to Microsoft Visual Basic
    ' For Applications Extensibility 5.3
    Dim mdc As Modules, md As Module
    Dim Cnt As Long, Lct As Long, Txt As String
    Dim DecLines As Long, ProcStLine As Long
    Dim ModName As String, PrName As String
    Dim ProcLine As Long
    
    Set mdc = Application.Modules
    ' Loop through all the modules in this collection
    For Cnt = 0 To mdc.Count - 1
        Set md = mdc(Cnt)
        ' Get name of module
        ModName = md.Name
        ' Get number of lines in declaration portion
        DecLines = md.CountOfDeclarationLines
        ' Loop through all the lines in this module
        For Lct = 1 To md.CountOfLines
            ' Get the contents of line Lct
            Txt = md.Lines(Lct, 1)
            If InStr(Txt, MatchString) > 0 Then
                If md.Type = acStandardModule _
                                            Or InStr(ModName, _
                                            "Form_") > 0 Then    ' (A)
                    If Lct > DecLines Then                      ' (B)
                        ' Get name of procedure
                        ' (in which, line Lct is contained)
                        PrName = md.ProcOfLine(Lct, _
                                            vbext_pk_Proc)
                        ' Get starting Line No for this procedure
                        ProcStLine = md.ProcBodyLine(PrName, _
                                            vbext_pk_Proc)        ' (C)
                        ' Compute Line number as counted
                        ' from start of procedure
                        ProcLine = Lct - ProcStLine + 1
                    Else
                        PrName = "Declaration Sec"
                        ProcStLine = 1
                        ProcLine = Lct
                    End If
                Else
                    PrName = "Class"
                    ProcStLine = 1
                    ProcLine = Lct
                End If
                Debug.Print "Mod - " & ModName & ",  Proc - " & _
                            PrName & ",  Line(ProcLine) - " & _
                            Lct & "(" & ProcLine & ")"
                Debug.Print Space(6) & Trim(Txt)
            End If
        Next
    Next
    
    Set md = Nothing
    Set mdc = Nothing
    
    ' Note - (A) & (B) are needed so as to ensure that there is
    '              no mismatch of argument regarding assumed type
    '              of procedure ()
    '              (C) The value returned by md.ProcBodyLine() is
    '              more dependable as it gives the line number where
    '              the procedure actually starts. On the other hand,
    '              md.ProcStartLine() returns the first line after end
    '              of prev procedure. This can be misleading when
    '              there are one or more blank lines between
    '              adjacent procedures.
End Sub
===================================

  ----- Original Message ----- 
  From: Rocky Smolin at Beach Access Software 
  To: 'Access Developers discussion and problem solving' 
  Sent: Saturday, March 10, 2007 21:39
  Subject: [AccessD] Looking at lines of code in code


  Dear List:
   
  I have a need to programmatically look at every line of code behind all
  forms looking for the string "TranslateMsgbox".
   
  I'm floundering around here a bit.  I know how to cycle through the forms collection.  But once I open the form in design view and Set frm = Forms(strFormName) I can't figure out how to loop through every line of code behind the form.  
   
  Any suggestions appreciated.
   
  MTIA
   
  Rocky



More information about the AccessD mailing list