Checking for a query

S

Steven Waugh

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?
 
A

Allen Browne

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?
 
T

Tim Ferguson

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
 
S

Steven Waugh

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.
 
A

Allen Browne

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top