How to fix "Run-time error '3012': Object 'MyQuery' already exists"

calc_pro 20 Reputation points

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.

A family of Microsoft relational database management systems designed for ease of use.
238 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,402 questions
Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
774 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Sheridan 2,496 Reputation points
    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

3 additional answers

Sort by: Most helpful
  1. Rahul Randive 5,656 Reputation points Microsoft Employee


    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!

    0 comments No comments

  2. Jiachen Li-MSFT 21,871 Reputation points Microsoft Vendor

    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.

    0 comments No comments

  3. calc_pro 20 Reputation points

    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
      MsgBox "An unexpected error has occurred. Please contact the database administrator."
      Exit Sub
    0 comments No comments