Delete query if it exists

J

Judy Ward

I am using the following code to delete a query:

DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name

which works if the query already exists, but crashes the subroutine if it
does not. I can't figure out a way to check if the query exists that doesn't
throw an error.

Is there anyone out there working on this holiday weekend that can help me
with this?

Thank you,
Judy
 
K

Ken Snell [MVP]

Normally, one would just trap the error and move on. Are you sure you cannot
use this method?

On Error Resume Next
DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name

To test for the presence of the query without allowing an error to occur
will involve a lot more code:

Dim qdf As DAO.QueryDefs
For Each qdf In DAO.Workspaces(0).Databases(0).QueryDefs
If qdf.Name = qry.Name Then
DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name
Exit For
End If
Next qdf
 
J

Judy Ward

Yes, I can trap the error and move on. Somehow I got the idea that I
shouldn't be creating an error. This will work. Thank you for responding!
Judy
 
J

John Vinson

I am using the following code to delete a query:

DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name

which works if the query already exists, but crashes the subroutine if it
does not. I can't figure out a way to check if the query exists that doesn't
throw an error.

So? Let it throw the error; trap it and ignore it. Simplest:

On Error Resume Next
DAO.Workspaces(0).Databases(0).QueryDefs.Delete qry.Name
On Error GoTo 0
Is there anyone out there working on this holiday weekend that can help me
with this?

<looking around> Holiday? Who takes holidays?

John W. Vinson[MVP]
 

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