retuen qry name if SQL contains certain text

gdesreu

Active Member
Joined
Jul 30, 2012
Messages
318
I would like to search through my database and return the query names of any queries that contain specific text. Is this doable? Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
i was always trying to find 'text' in a query , so I wrote this:

usage:
FindinSql "bob"

it prints all the results, in the debug space,
Code:
Public Sub FindInSql(pvFind, Optional ByVal pvQType)
'Dim db As Database
Dim qdf As QueryDef
Dim vFind
Dim sSql As String
'qtype can be..
Const kQS = 0
Const kQD = 32
Const kQU = 48
Const kQA = 64
Const kQM = 80
Const kQN = 128
Const kQX = 16
vFind = pvFind   '"tRptDefectsBlocked"


'Set db = CurrentDb
Debug.Print "-- Start qry find in:" & vFind
'Debug.Print "qry type FILTER:="


For Each qdf In CurrentDb.QueryDefs
    sSql = qdf.SQL
    qdf.Close
    If InStr(sSql, vFind) > 0 Then
       If IsMissing(pvQType) Then
          Debug.Print getQryTypeNam(qdf.Type); ": "; qdf.Name
       Else
          If qdf.Type = pvQType Then Debug.Print getQryTypeNam(qdf.Type); ": "; qdf.Name             ', "..FLT SRCH"
       End If
    End If
Next
Debug.Print "--End qry find"
Set qdf = Nothing
'Set db = Nothing
End Sub

 'qry types in English
Public Function getQryTypeNam(pvTypeNum)
Select Case pvTypeNum
    Case 0 '= select
       getQryTypeNam = "sel"
    Case 16 '= xtab
       getQryTypeNam = "xtab"
    Case 32 '= del
       getQryTypeNam = "del"
    Case 48 '= upd
       getQryTypeNam = "upd"
    Case 64 '= appd
       getQryTypeNam = "apd"
    Case 80 '= make
       getQryTypeNam = "make"
    Case 128 '= union
       getQryTypeNam = "union"
End Select


End Function

i added a param in case i only wanted to search in Update queries.
 
Last edited:
Upvote 0
i was always trying to find 'text' in a query , so I wrote this:

usage:
FindinSql "bob"

it prints all the results, in the debug space,
Code:
Public Sub FindInSql(pvFind, Optional ByVal pvQType)
'Dim db As Database
Dim qdf As QueryDef
Dim vFind
Dim sSql As String
'qtype can be..
Const kQS = 0
Const kQD = 32
Const kQU = 48
Const kQA = 64
Const kQM = 80
Const kQN = 128
Const kQX = 16
vFind = pvFind   '"tRptDefectsBlocked"


'Set db = CurrentDb
Debug.Print "-- Start qry find in:" & vFind
'Debug.Print "qry type FILTER:="


For Each qdf In CurrentDb.QueryDefs
    sSql = qdf.SQL
    qdf.Close
    If InStr(sSql, vFind) > 0 Then
       If IsMissing(pvQType) Then
          Debug.Print getQryTypeNam(qdf.Type); ": "; qdf.Name
       Else
          If qdf.Type = pvQType Then Debug.Print getQryTypeNam(qdf.Type); ": "; qdf.Name             ', "..FLT SRCH"
       End If
    End If
Next
Debug.Print "--End qry find"
Set qdf = Nothing
'Set db = Nothing
End Sub

 'qry types in English
Public Function getQryTypeNam(pvTypeNum)
Select Case pvTypeNum
    Case 0 '= select
       getQryTypeNam = "sel"
    Case 16 '= xtab
       getQryTypeNam = "xtab"
    Case 32 '= del
       getQryTypeNam = "del"
    Case 48 '= upd
       getQryTypeNam = "upd"
    Case 64 '= appd
       getQryTypeNam = "apd"
    Case 80 '= make
       getQryTypeNam = "make"
    Case 128 '= union
       getQryTypeNam = "union"
End Select


End Function

i added a param in case i only wanted to search in Update queries.
Thanks, I'm getting a compile error sub or function not defined at getQryTypeNam"
 
Upvote 0
i was always trying to find 'text' in a query , so I wrote this:

usage:
FindinSql "bob"

it prints all the results, in the debug space,
Code:
Public Sub FindInSql(pvFind, Optional ByVal pvQType)
'Dim db As Database
Dim qdf As QueryDef
Dim vFind
Dim sSql As String
'qtype can be..
Const kQS = 0
Const kQD = 32
Const kQU = 48
Const kQA = 64
Const kQM = 80
Const kQN = 128
Const kQX = 16
vFind = pvFind   '"tRptDefectsBlocked"


'Set db = CurrentDb
Debug.Print "-- Start qry find in:" & vFind
'Debug.Print "qry type FILTER:="


For Each qdf In CurrentDb.QueryDefs
    sSql = qdf.SQL
    qdf.Close
    If InStr(sSql, vFind) > 0 Then
       If IsMissing(pvQType) Then
          Debug.Print getQryTypeNam(qdf.Type); ": "; qdf.Name
       Else
          If qdf.Type = pvQType Then Debug.Print getQryTypeNam(qdf.Type); ": "; qdf.Name             ', "..FLT SRCH"
       End If
    End If
Next
Debug.Print "--End qry find"
Set qdf = Nothing
'Set db = Nothing
End Sub

 'qry types in English
Public Function getQryTypeNam(pvTypeNum)
Select Case pvTypeNum
    Case 0 '= select
       getQryTypeNam = "sel"
    Case 16 '= xtab
       getQryTypeNam = "xtab"
    Case 32 '= del
       getQryTypeNam = "del"
    Case 48 '= upd
       getQryTypeNam = "upd"
    Case 64 '= appd
       getQryTypeNam = "apd"
    Case 80 '= make
       getQryTypeNam = "make"
    Case 128 '= union
       getQryTypeNam = "union"
End Select


End Function

i added a param in case i only wanted to search in Update queries.
Nevermind it works perfectly, (It helps to put the second function there) My bad...red cheeks. Thanks this is perfect absolutely perfect for what I need.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top