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

calc_pro 20 Reputation points
2023-04-27T11:47:50.7966667+00:00

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.

Access
Access
A family of Microsoft relational database management systems designed for ease of use.
393 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,728 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.
878 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ken Sheridan 2,841 Reputation points
    2023-04-29T21:19:25.6533333+00:00
    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. 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.

    https://www.errorvault.com/en/troubleshooting/runtime-errors/microsoft/microsoft-access/error-3012_microsoft-access-error-3012

    If you find it unhelpful, feel free to reach out to here.

    Thank you!

    0 comments No comments

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

    0 comments No comments

  3. 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
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.