Checking for a query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What coding is required to check if query "X" currently exists. If it does exist, delete it and move on to the next step. If it doesn't, move on to the next step?
 
See if you get an error by referring to it, i.e. see if:
CurrentDb().QueryDefs("X")
produces error 3265.

If the next step is to create another similar query, you might want to
consider just changing the SQL property of the QueryDef instead.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Steven Waugh said:
What coding is required to check if query "X" currently exists. If it does
exist, delete it and move on to the next step. If it doesn't, move on to the
next step?
 
If it does exist, delete it and move on to the next step. If it
doesn't, move on to the next step?

Or just ignore the "if" part altogether:

On Error Resume Next
QueryDefs.Delete "MyUnwantedQuery"


On Error GoTo 0
' and so on...


HTH


Tim F
 
I am new to the programming game. Is there any way you could expand on what you were saying?

I understand what you mean, but I don't know how to implement it.
 
Paste this into a general module:

Public Function QueryExists(QueryName As String) As Boolean
Dim varDummy As DAO.QueryDef
On Error Resume Next
Set varDummy = CurrentDb().QueryDefs(QueryName)
QueryExists = (Err.Number = 0)
End Function

You can now use:
If QueryExists("X") Then
MsgBox "Yes"
Else
MsgBox "No
End If
 
Back
Top