Microsoft KB Archive/244695

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

Article ID: 244695

Article Last Modified on 7/14/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q244695



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 that this is a bug in the Microsoft products that are 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
 Start Microsoft Access and create a new database. Save the database to c:\db1.mdb. Create a blank form and name it Form1 . Select Tools from the Startup menu and deselect Display Database Window. Quit Access. Start Visual Basic and create a new Standard EXE project. Form1 is created by default.</li> On the Project menu, choose References and select the Microsoft Access 9.0 Object Library.</li> Add a CommandButton control (Command1) to Form1.</li>  Add the following code to the form's code module: 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 </li> 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.</li></ol>

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: <ol> Start Microsoft Access and open c:\db1.mdb.</li> Press the ALT+F11 key combination to view the Visual Basic Editor.</li> Insert a new Module (Module1).</li>  Add the following code to Module1: Public Sub OpenAccForm(ByVal sForm As String) Application.DoCmd.OpenForm sForm End Sub </li> Save the module as Module1 and exit Access.</li>  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: 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 </li></ol>

Additional query words: acc2000 2046

Keywords: kbbug kbautomation kbpending KB244695

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.