how to find if there is a query "name" in current access DB?

  • Thread starter Thread starter Jerry Qu
  • Start date Start date
Dim db as DAO.Database
Dim qdf as DAO.QueryDef

Set db = CurrentDb
On Error Resume Next
' The next line will raise an error if the query doesn't exist
Set qdf = db.QueryDefs("qryName")

' Test for an error. If Error.Number = 0, there is no error.
If Err.Number = 0
MsgBox "qryName exists"
Else
MsgBox "qryName does not exist"
End If

'On Error....... (Optionally, Restore your regular ErrorHandling, which also
resets Err.Number to 0)

Set qdf = Nothing
Set db = Nothing

--
HTH,

George Nicholson

(Please post responses to newsgroup but remove "Junk" from return address if
used)
 
Jerry said:
how to find if there is a query "qryname" existing in current access DB?

TIA

Jerry

Jerry, you need to interact with the Access collection object. Place the
following code in a Module. Save the module and use the function anywhere by
typing DoesQueryExist("Query1"). The function will return True if yes or
False if "Query1" does not exist within the current database.

Public Function DoesQueryExist(qryname As String) As Boolean
Dim qry As AccessObject
Dim dbs As Access.CurrentData
Set dbs = Application.CurrentData
For Each qry In dbs.AllQueries
If qry.Name = qryname Then
DoesQueryExist = True
Exit For
End If
Next qry
End Function
 
Back
Top