Solved

How to search for strings in VBA Ccode

Posted on 2011-02-27
4
343 Views
Last Modified: 2013-11-27
How can I use VBA Code to check for the string:
   Const ThisProcName as string = "XXX"
in every procedure of every module of my mdb, and return the name of the first module/ procedure that doesn't have it?
NOTE: 'XXX' is the name of each procedure.
0
Comment
Question by:Milewskp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 500 total points
ID: 34991711
As a starter for 10 check out Chips page here.

http://www.cpearson.com/excel/vbe.aspx

In more specific detail, what are you trying to do ... searh every module and every sub in each module to make sure the string "XXX" is referenced in the subs?  If so I assume you include functions in the scenario?

Chris
0
 
LVL 1

Author Comment

by:Milewskp
ID: 34993068
hi chris,
<what are you trying to do ... searh every module and every sub in each module to make sure the string "XXX" is referenced in the subs?  If so I assume you include functions in the scenario?>
Yes, every procedure, including subs, functions and event procedures.

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34995695
The following ought to do as requested:

Const kstrFindString As String = "XXX"
 
is teh string you want to see if it is present in each sub/function

findModules is the sub to call with the relevant database being open open and active.

You stated you want to know the first sub without the string so the final line:

    msgbox arr(0)

dose just that ... but the array does contain every sub/function in teh normal code modules where the string was NOT found.  You can process it as required of course as the string identifies the module name and the code routine name.

Note in order to test I added a dummy line in the ListProcedures to avoid false triggers in that sub ... you can of course delete that line if required ... but if you want to retain it then you need to replace XXX in both subs with your real search string.

Chris
Sub findModules()
Dim VBProj As Object
Dim VBComp As Object
Dim arr As Variant
Const kstrFindString As String = "XXX"
    
    ReDim arr(0)
    arr(0) = "NOT found"
    Set VBProj = Application.VBE.ActiveVBProject
    For Each VBComp In VBProj.VBComponents
        Select Case VBComp.Type
            Case 11 'vbext_ct_ActiveXDesigner
                'ActiveX
            Case 2 'vbext_ct_ClassModule
                'Class Module
            Case 100 'vbext_ct_Document
                'Document Module
            Case 3 'vbext_ct_MSForm
                'UserForm
            Case 1 'vbext_ct_StdModule
                'Code Module
                arr = ListProcedures(VBComp.Name, arr, kstrFindString)
            Case Else
                'Unknown Type
        End Select
    Next VBComp
    Debug.Print arr(0)

End Sub

Function ListProcedures(strModuleName As String, arr As Variant, strFindMe) As Variant
Dim VBProj As Object
Dim VBComp As Object
Dim CodeMod As Object
Dim ProcName As String
Dim ProcKind As Long
Dim lngBodyLine As Long
Dim lngStartLine As Long
Dim lngProcLines As Long
Dim intArraySize As Integer
        
    'XXX - Stop false triggers!
    Set VBProj = Application.VBE.ActiveVBProject
    Set VBComp = VBProj.VBComponents(strModuleName)
    Set CodeMod = VBComp.CodeModule
        
    ProcKind = 0
    intArraySize = UBound(arr)
    With CodeMod
        lngBodyLine = .CountOfDeclarationLines + 1
        Do Until lngBodyLine >= .CountOfLines
            ProcName = .ProcOfLine(lngBodyLine, ProcKind)
            lngStartLine = .ProcStartLine(ProcName, ProcKind)
            lngBodyLine = .ProcBodyLine(ProcName, ProcKind)
            lngProcLines = .ProcCountLines(ProcName, ProcKind)
            lngStartLine = lngStartLine + lngProcLines + 1
            If Not .Find(strFindMe, lngBodyLine, 1, lngBodyLine + lngProcLines, -1, True, False) Then
                ReDim Preserve arr(0 To intArraySize)
                arr(intArraySize) = strModuleName & ":" & ProcName
                intArraySize = intArraySize + 1
            End If
            lngBodyLine = lngStartLine
        Loop
    End With
    ListProcedures = arr

End Function

Open in new window

0
 
LVL 1

Author Comment

by:Milewskp
ID: 35006565
Thanks Chris,
That's what I was looking for. Great link too!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question