BUG: Error Message 2046 Calling OpenForm or OpenReport with Access Automation

ID: Q244695


The information in this article applies to:
  • Microsoft Access 2000


SYMPTOMS

When you automate Microsoft Access and call either the OpenForm or OpenReport method, you receive one of the following errors:

Run-time error '2046':
The command or action 'OpenForm' isn't available now.
-or-
Run-time error '2046':
The command or action 'OpenReport' isn't available now.


CAUSE

This problem occurs when the Database window is hidden at the time you call the OpenForm or OpenReport method from automation.


RESOLUTION

You can work around this problem by doing one of the following:

  • Make sure the Database window is shown before calling the OpenForm or OpenReport method. See "Workaround 1" below for a code sample.


  • Create a module in the Access database that contains the code to open the form or report, then call that procedure from automation using the Run method. See "Workaround 2" below for a code sample.



STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

When you open a database in Microsoft Access, you have the option to hide the Database window for that database. For example, open your database in Microsoft Access, chose Startup from the Tools menu, and deselect the Display Database Window checkbox. The next time this database is opened in Access, the Database window is not displayed by default. If the Database window is not displayed when you call the OpenForm or OpenReport method from automation, you experience the run-time error.

Steps to Reproduce Behavior

  1. Start Microsoft Access and create a new database. Save the database to c:\db1.mdb.


  2. Create a blank form and name it Form1.


  3. Select Tools from the Startup menu and deselect Display Database Window.


  4. Quit Access.


  5. Start Visual Basic and create a new Standard EXE project. Form1 is created by default.


  6. On the Project menu, choose References and select the Microsoft Access 9.0 Object Library.


  7. Add a CommandButton control (Command1) to Form1.


  8. Add the following code to the form's code module:


  9. 
    Option Explicit
    Private oAccess As Access.Application
    
    Private Sub Command1_Click()
        Set oAccess = CreateObject("Access.Application")
        oAccess.Visible = True
        oAccess.OpenCurrentDatabase "c:\db1.mdb"
        oAccess.DoCmd.OpenForm "Form1"
    End Sub 
  10. Press the F5 key to run the project, and then click the CommandButton.

    Result: An error occurs on the OpenForm method because the Database window for db1.mdb is hidden.


Workarounds

There are two known workarounds for this problem. Both workarounds are described below.

Workaround 1

This workaround shows how to unhide the Database window before calling the OpenForm or OpenReport method, then optionally re-hide the Database window:

Option Explicit
Private oAccess As Access.Application

Private Sub Command1_Click()
    Set oAccess = CreateObject("Access.Application")
    oAccess.Visible = True
    oAccess.OpenCurrentDatabase "c:\db1.mdb"
    
    ' Unhide the Database window by selecting any
    '  database object in the Database window, such as
    '  the form or report you want to open:
    oAccess.DoCmd.SelectObject ObjectType:=acForm, _
        ObjectName:="Form1", InDatabaseWindow:=True
        
    ' Open the desired form or report:
    oAccess.DoCmd.OpenForm "Form1"
    
    ' Hide the Database window (optional):
    oAccess.DoCmd.SelectObject ObjectType:=acForm, _
        ObjectName:="Form1", InDatabaseWindow:=True
    oAccess.RunCommand acCmdWindowHide
End Sub 

Workaround 2

Another workaround for this problem is to create a module in the Access database and add a procedure to that module that contains the code to open the form or report. Then, call this procedure from automation using the Run method. The following steps provide an example:
  1. Start Microsoft Access and open c:\db1.mdb.


  2. Press the ALT+F11 key combination to view the Visual Basic Editor.


  3. Insert a new Module (Module1).


  4. Add the following code to Module1:


  5. 
    Public Sub OpenAccForm(ByVal sForm As String)
        Application.DoCmd.OpenForm sForm
    End Sub 
  6. Save the module as Module1 and exit Access.


  7. Now try the following automation code. This automation code calls the OpenAccForm procedure. This allows the Database window to remain hidden, yet still open the form or report without the error:


  8. 
    Option Explicit
    Private oAccess As Access.Application
    
    Private Sub Command1_Click()
        Set oAccess = CreateObject("Access.Application")
        oAccess.Visible = True
        oAccess.OpenCurrentDatabase "c:\db1.mdb"
        oAccess.Run "OpenAccForm", "Form1"
    End Sub 

Additional query words: acc2000 2046

Keywords : kbAccess kbAutomation KbVBA kbGrpDSO kbDSupport
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: November 21, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.