In situations like this I would recommend that the querydef object be deleted before calling the CreateQueryDef method, handling and ignoring the error if the query does not exist:
Const QUERYDOESNOTEXIST = 3265
On Error Resume Next
CurrentDb.QueryDefs.Delete "MyQuery"
Select Case Err.Number
Case 0
' no error
Case QUERYDOESNOTEXIST
' anticipated error, ignore
Case Else
MsgBox "An unexpected error has occurred. Please contact the database administrator."
End Select
DoCmd.Close acForm, "folderForm", acSaveNo
How to fix "Run-time error '3012': Object 'MyQuery' already exists"
Hi everyone, I was trying to update some information on the database using Form and suddenly I got an error message saying, "Run-time error '3012': Object 'MyQuery' already exists". It was working perfectly fine before till now. I have a little knowledge in Microsoft Access, and I will appreciate if anyone can help me with this issue. Thank you very much in advance.
-
Ken Sheridan 2,841 Reputation points
2023-04-29T21:19:25.6533333+00:00
3 additional answers
Sort by: Most helpful
-
RahulRandive 9,666 Reputation points
2023-04-27T21:36:19.27+00:00 Hi,
Thanks for your question.
Please check below blog for possible solution to an error you are getting.
If you find it unhelpful, feel free to reach out to here.
Thank you!
-
Jiachen Li-MSFT 31,411 Reputation points Microsoft Vendor
2023-04-28T05:33:57.02+00:00 Hi @calc_pro ,
It is recommend that you determine whether a new query already exists before creating a new query in your code.
Could you please provide more information about the code in which the error occurred and what you did when the error occurred.
-
calc_pro 20 Reputation points
2023-04-28T13:43:18.8966667+00:00 Thank you very much for your prompt response both of you. The thing is that I did not create this database, but based on what I was told they were getting this same error often however it would fix automatically after restarting the PC. But this is not the case now because it's been more than 3 days, and I'm still getting the same error. This form was working fine couple of days ago, I was able to update the information until it suddenly stopped working.
After the error message I click debug button and it highlights line of code which starts with set temp. I tried compact and repair the database. I also tried to create new database and import queries, forms, tables from source database, nothing fixed the issue.
Thank you very much again.
End If '''On Error GoTo PROC_ERR Set temp = CurrentDb.CreateQueryDef("MyQuery", "SELECT * FROM folder") db.Execute stmt, dbFailOnError CurrentDb.QueryDefs.Delete "MyQuery" DoCmd.Close acForm, "folderForm", acSaveNo Exit Sub PROC_ERR: MsgBox "An unexpected error has occurred. Please contact the database administrator." Exit Sub