Solved

How to search for strings in VBA Ccode

Posted on 2011-02-27
4
334 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now