Solved Searching query text.

John Sh

Active member
Local time
Today, 11:44
Joined
Feb 8, 2021
Messages
612
Is there an easy way to search the text of multiple queries for a particular string?
I have a situation where a query is requesting input but I can't track down which query is the culprit.
 
Sub dumpQueriesWithSQL(ByVal sql As String)
Dim db As DAO.Database
Dim qd As DAO.QueryDef

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.sql Like "*" & sql & "*" Then Debug.Print qd.Name
Next qd
End Sub
 
I found a sub by "GUUS" in 2007 that fills the bill nicely.
It scrolls through al qdf's and returns each query with the search string in a message box.
 
Sub dumpQueriesWithSQL(ByVal sql As String)
Dim db As DAO.Database
Dim qd As DAO.QueryDef

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.sql Like "*" & sql & "*" Then Debug.Print qd.Name
Next qd
End Sub
Thanks Tom. It certainly does what is required. The other sub lists the query code as well so is a bit more versatile.
Thank you for responding.
John
 
Sub dumpQueriesWithSQL(ByVal sql As String)
Dim db As DAO.Database
Dim qd As DAO.QueryDef

Set db = CurrentDb
For Each qd In db.QueryDefs
If qd.sql Like "*" & sql & "*" Then Debug.Print qd.Name
Next qd
End Sub
I have altered you sub to add the found query names to a table. This makes it easier to check the queries at a later time.
John

Code:
Private Sub dumpQueriesWithSQL(ByVal sql As String)
    Dim db As DAO.Database
    Dim qd As DAO.QueryDef
    DoCmd.SetWarnings False
        DoCmd.RunSQL "DELETE * FROM qtemp"
        Set db = CurrentDb
        For Each qd In db.QueryDefs
            If qd.sql Like "*" & sql & "*" Then DoCmd.RunSQL "INSERT INTO [qtemp] ([query]) VALUES ('" & qd.Name & "' );"
        Next qd
    DoCmd.SetWarnings True
End Sub
 
I have taken this a step further and created a form that searches all queries for the given text then prints out the text of the query into a textbox.
Select any query from the dropdown to see it's code.
I have left popup and modal as no., allowing the queries to be accessed while the form is still active.
Hopefully this will help someone else with my predicament.
John
 

Attachments

Users who are viewing this thread

Back
Top Bottom