Need way to check if a query exists.

marathonlady

Registered User.
Local time
Yesterday, 21:57
Joined
Jul 10, 2002
Messages
120
How do I programatically check to see if a query exists?

Thanks.
 
Code:
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentData

    For Each obj In dbs.AllQueries
       If obj.Name = "QryWhatEver" Then
            Debug.Print obj.Name
        End If
    Next obj
Regards,
Tim
 
one way would be


Dim dbs As DAO.Database
Dim Rst As DAO.Recordset
dim MyQueryName as String
Set dbs = CurrentDb()
Dim QDF As DAO.QueryDef
Dim PRM As Parameter
MyQueryName="thenameof thequery"

on error goto NoQuery:

Set QDF = dbs.QueryDefs(MyQueryName)
For Each PRM In QDF.Parameters
PRM.Value = Eval(PRM.name)
Next PRM
Set Rst = QDF.OpenRecordset(dbOpenDynaset)
With Rst
If Rst.BOF And Rst.EOF Then

'the query exists but has no records
Rst.Close
Set Rst = Nothing
Set dbs = Nothing


Else:
'if you want to do anything do it here
End If
End With
Rst.Close

ExitHere
Set Rst = Nothing
Set dbs = Nothing
ExitSub

NoQuery:
if err.number=7874 then
msgbox "Cant Find Query"
resume ExitHere:

else
msgbox err.description
resume ExitHere:

end if
 

Users who are viewing this thread

Back
Top Bottom