Error message on “object already exists†when running a query

  • Thread starter hkgary33 via AccessMonster.com
  • Start date
H

hkgary33 via AccessMonster.com

Dear all,
In a button’s click event, I’ve built a procedure such that once the button
is clicked, a specific query will be run either in Access or Excel. The codes
are as follows:
Dim qdfs As QueryDefs
Dim qdf As QueryDef
Dim dbf As Database
Dim strSQL As String
Set dbf = CurrentDb
Set qdfs = dbf.QueryDefs
strSQL = "SELECT …." ‘ a SQL query statement (details not shown here)
Set qdf = dbf.CreateQueryDef("Query Result", strSQL)
If opt_output = 1 Then 'Option choice that Output to Excel
DoCmd.OutputTo acOutputQuery, qdf.NAME, acFormatXLS, , -1
Else 'Option choice that Output to Access
DoCmd.OpenQuery qdf.NAME
End If
qdfs.Delete qdf.NAME
….


The above codes runs successfully for the first time, but the serious problem
is that once I close the whole mdb file and then launch it in later time, a
pop-up message shown and then the query result won’t be displayed after
clicking the button. The error message said:
“Object ‘Query Result’ already existsâ€

I really don’t know what’s going on, since I’ve already used the Delete
action in the last line of code, so the query should be deleted at last, but
when I switch to the query panel, the “Query Result†query still exists.
Please, can anyone solve this serious problem?

Thanks so much!!!!!

Gary
 
E

E

dont know why its not deleting.
but the safe thing to do, i guess, is check the collection for your querydef
before creating it. at least that way you could verify if the querydef does
exist.
 
H

hkgary33 via AccessMonster.com

icic....then can you tell me how to check it by using VBA code?
Or, should I use the Delete Object action rather than Delete?
Thanks !!!!
Gary
dont know why its not deleting.
but the safe thing to do, i guess, is check the collection for your querydef
before creating it. at least that way you could verify if the querydef does
exist.
Dear all,
In a button’s click event, I’ve built a procedure such that once the button
[quoted text clipped - 30 lines]
 

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