Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to search for strings in VBA Ccode

Posted on 2011-02-27
4
Medium Priority
?
346 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 2000 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Suggested Courses

618 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