Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for String in ALL Code withing Forms, Reports and modules 2

Status
Not open for further replies.

Poduska

Technical User
Dec 3, 2002
108
US
I need to search database code for instances of text within forms, Reports or public modules. I am attempting to find table and field names in code which may be referenced in SQL queries, dlookups etc. built into the code.

I have a nice function which looks through SQL statements in queries and searches data sources for controls on forms and reports but I am missing the background code.

[blue]
I am trying to get at the AfterUpdates, Before Updates, lostfocus and similar events along with anything in the public modules. [/blue]


[red]
I need to make a tool I can use to be confident I can locate everywhere a string is referenced. This will be handy when I have to change a name or delete a field or table.[/red]


This is a bit of the code I use for looking through reports to illustrate what I am attempting to accomplish.

This searches the source SQL for all controls on a report looking for any occurrence of a prompted sting. If any are found the function then writes where the string was located to a table I open earlier in the code. This works GREAT and fast.


Code:
            For Each ctl In rpt.Controls
                Select Case ctl.ControlType
                Case acComboBox, acListBox
                    If InStr(1, ctl.RowSource, strSearch) > 0 Then
                        With rec
                            .AddNew
                            !searchString = strSearch
                            !DateCreated = obj.DateCreated
                            !ReportName = rpt.Name
                            !ControlName = ctl.Name
                            !SQL = ctl.RowSource
                            .Update
                        End With
                    End If
                Case acTextBox
                    If InStr(1, ctl.ControlSource, strSearch) > 0 Then
                        With rec
                            .AddNew
                            !searchString = strSearch
                            !DateCreated = obj.DateCreated
                            !ReportName = rpt.Name
                            !ControlName = ctl.Name
                            !SQL = ctl.ControlSource
                            .Update
                        End With
                    End If

                End Select
            Next ctl

How do I loop through all the Code??

This is the entire code for reference if someone finds this on a search, perhaps it could be usefull.
Code:
Sub SearchSQL()
    Dim tdf As TableDef
    Dim qdf As QueryDef
    Dim rec As Recordset
    Dim frm As Form
    Dim rpt As Report
    Dim obj As AccessObject
    Dim ctl As Control
    Dim strSQL As String
    Dim strSearch As String
     
     
    On Error GoTo ErrHandler
    strSearch = InputBox("Enter the search string", "Search ALL SQL")
     
    If strSearch <> "" Then
        DoCmd.Hourglass True
        Application.Echo False
         
         ' create a table to hold the list
        Set tdf = CurrentDb.CreateTableDef("ztblSQLSearch")
         
        With tdf
            .Fields.Append .CreateField("SearchString", dbText)
            .Fields.Append .CreateField("DateCreated", dbDate)
            .Fields.Append .CreateField("FormName", dbText)
            .Fields.Append .CreateField("ReportName", dbText)
            .Fields.Append .CreateField("ControlName", dbText)
            .Fields.Append .CreateField("QueryName", dbText)
            .Fields.Append .CreateField("SQL", dbMemo)
        End With
         
        CurrentDb.TableDefs.Append tdf
         
        Set rec = CurrentDb.OpenRecordset("ztblSQLSearch", dbOpenDynaset)
         
         ' List all queries
         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        For Each qdf In CurrentDb.QueryDefs
             'exclude all temporary queries
            If Left(qdf.Name, 1) <> "~" Then
                 'check for the search string
                If InStr(1, qdf.SQL, strSearch) > 0 Then
                    With rec
                        .AddNew
                        !searchString = strSearch
                        !DateCreated = qdf.DateCreated
                        !QueryName = qdf.Name
                        !SQL = qdf.SQL
                        .Update
                    End With
                End If
            End If
        Next qdf
         
         ' List all forms
         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        For Each obj In Application.CurrentProject.AllForms
             ' check for the search string
            If Not obj.IsLoaded Then
                DoCmd.OpenForm obj.Name, acDesign
                Set frm = Application.Screen.ActiveForm
            Else
                Set frm = Forms(obj.Name)
            End If
            
     ' Debug.Print frm.Name
      Debug.Print frm.Name & "  " & frm.RecordSource
    
            If InStr(1, frm.RecordSource, strSearch) > 0 Then
                With rec
                    .AddNew
                    !searchString = strSearch
                    !DateCreated = obj.DateCreated
                    !FormName = frm.Name
                    !SQL = frm.RecordSource
                    .Update
                End With
            End If
             
             ' check each combobox and listbox
            For Each ctl In frm.Controls
                Select Case ctl.ControlType
                
                Case acComboBox, acListBox
                    If InStr(1, ctl.RowSource, strSearch) > 0 Then
                        With rec
                            .AddNew
                            !searchString = strSearch
                            !DateCreated = obj.DateCreated
                            !FormName = frm.Name
                            !ControlName = ctl.Name
                            !SQL = ctl.RowSource
                            .Update
                        End With
                    End If
                Case acTextBox
                    If InStr(1, ctl.ControlSource, strSearch) > 0 Then
                        With rec
                            .AddNew
                            !searchString = strSearch
                            !DateCreated = obj.DateCreated
                            !FormName = frm.Name
                            !ControlName = ctl.Name
                            !SQL = ctl.ControlSource
                            .Update
                        End With
                    End If
                End Select
            Next ctl
             
            DoCmd.Close acForm, frm.Name, acSaveNo
        Next obj
         
         ' List all reports
         '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        For Each obj In Application.CurrentProject.AllReports
             ' check for the search string
            If Not obj.IsLoaded Then
                DoCmd.OpenReport obj.Name, acDesign
                Set rpt = Application.Screen.ActiveReport
            Else
                Set rpt = Reports(obj.Name)
            End If
            
    '  Debug.Print rpt.Name
    '  Debug.Print rpt.RecordSource
            
            If InStr(1, rpt.RecordSource, strSearch) > 0 Then
                With rec
                    .AddNew
                    !searchString = strSearch
                    !DateCreated = obj.DateCreated
                    !ReportName = rpt.Name
                    !SQL = rpt.RecordSource
                    .Update
                End With
            End If
             
             ' check each combobox,listbox or textBox
            For Each ctl In rpt.Controls
                Select Case ctl.ControlType
                Case acComboBox, acListBox
                    If InStr(1, ctl.RowSource, strSearch) > 0 Then
                        With rec
                            .AddNew
                            !searchString = strSearch
                            !DateCreated = obj.DateCreated
                            !ReportName = rpt.Name
                            !ControlName = ctl.Name
                            !SQL = ctl.RowSource
                            .Update
                        End With
                    End If
                Case acTextBox
                    If InStr(1, ctl.ControlSource, strSearch) > 0 Then
                        With rec
                            .AddNew
                            !searchString = strSearch
                            !DateCreated = obj.DateCreated
                            !ReportName = rpt.Name
                            !ControlName = ctl.Name
                            !SQL = ctl.ControlSource
                            .Update
                        End With
                    End If

                End Select
            Next ctl
             
            DoCmd.Close acReport, rpt.Name, acSaveNo
        Next obj
    End If
     
    DoCmd.OpenTable "ztblSQLSearch"
     
     
ExitHere:
    On Error Resume Next
    rec.Close
    Set rec = Nothing
    Set qdf = Nothing
    Set tdf = Nothing
    Set obj = Nothing
    Set frm = Nothing
    Set rpt = Nothing
     
    DoCmd.Hourglass False
    Application.Echo True
    Exit Sub
     
     
ErrHandler:
    Select Case Err.Number
    Case 3010
        On Error Resume Next
        CurrentDb.TableDefs.Delete "ztblSQLSearch"
        Err.Clear
        Resume
         
    Case Else
        MsgBox Err.Number & ": " & Err.Description
        Err.Clear
        GoTo ExitHere
    End Select
End Sub
 
How are ya Poduska . . .

Have a look at Search Wizard

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
I imagine the Search Wizard will do what you need, but I think their is a FAQ on this site as well. I could not find it, though. If I am correct maybe someone can point it out.
 
Howdy MajP . . .

I've seen it myself but I can't find it either!

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
WOW this sounds and looks like it will be exactly what I am looking to use. A star for the help!

Unfortunatley, although it appears that someone has put a ton of work into this I receive errors when the form opens and it never seems to find anything as I always receive error
Code:
2467 The expression you entered refers to an object that is closed or dosen't exist

I tried it as a stand alone and copied everything into Northwinds with same results even loking for the string "orders"

Perhaps I will try and download it again.

 
For an illustration of how to get VBA code from modules and event handlers, plus the description properties of objects take a look at the source code for MDB Doc

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top