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.
- On the Project menu, choose References and select the Microsoft Access 9.0 Object Library.
- Add a CommandButton control (Command1) to Form1.
- 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
- 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:
- Start Microsoft Access and open c:\db1.mdb.
- Press the ALT+F11 key combination to view the Visual Basic Editor.
- Insert a new Module (Module1).
- Add the following code to Module1:
Public Sub OpenAccForm(ByVal sForm As String)
Application.DoCmd.OpenForm sForm
End Sub
- Save the module as Module1 and exit Access.
- 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