Solved Search all objects & properties for specific sql string

adhoustonj

Member
Local time
Today, 18:13
Joined
Sep 23, 2022
Messages
192
Hello AWF,
Does anyone have any experience or code with searching all database objects for a sql string? I've started looking at how to approach this over the last few days and haven't come up with much.. Other than the export to text and then search through text files.

Basically - I've migrated some back end databases to SQL Server, and I'm noticing one query that is an outlier when viewing the top resource consuming queries on the server, but I can not find the query in my front end app! So I've scratched my head a few times, and rather than go one by one searching each object, I was hoping to be able to search all via vba.
 
The deep search function in V-tools will find almost anything.
 
Not as sophisticated, but I have a routine to generate the list of all queries in the database to a table "T_SysSQLCollection" at the time of launching the report.
The table:
1687876800202.png

The report
1687877003485.png

The queries cover those embedded in forms, subforms, reports and queries
I have not set up a search SQL capability but easily done.
Code:
Public Function ListQueries()

'---------------------------------------------------------------------------------------
' Procedure : ListQueries
' DateTime  : 2020 - Sep - 16
' Author    : Graham Pegler
' Purpose   : To create documentation for all queries/as SQL in this database - populates table to hold the name and SQL _
'               of each query, before generating a report preview to incorporate in system docs.
'
'**** Note: This routine deletes all records in the table t_SQLCollection on each execution before regenerating the records ****

Dim db As DAO.Database
Dim rst As DAO.recordSet
Dim qdf As DAO.QueryDef

Dim i As Integer
Dim QueryName, QName, strQryDel As String

On Error Resume Next

' SQL to clear records
strQryDel = "DELETE t_SysSQLCollection.* FROM t_SysSQLCollection;"

On Error GoTo Err_ListQueries

'   Clean out the existing records before repopulating

Set db = CurrentDb()

db.Execute strQryDel, dbFailOnError
DoEvents
        
Set rst = db.OpenRecordset("t_SysSQLCollection")
i = 0
With rst
    For Each qdf In db.QueryDefs
    .AddNew
        !QueryName = qdf.Name
        !qSQL = qdf.sQL
        !qDescription = qdf.Properties("description")
        .Update
        i = i + 1
    Next
End With
    
MsgBox "Finished - " & i & " Queries have been documented", vbInformation, "QUERY LIST"

rst.Close
db.Close
 

Users who are viewing this thread

Back
Top Bottom