Solved

How to search for strings in VBA Ccode

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

680 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