You can use the OpenCurrentDatabase method to open an existing database as the current database. You can use the this method to open a database from another application that is controlling Microsoft Access through OLE Automation. For example, you can use the OpenCurrentDatabase method from Microsoft Excel to open the Northwind sample database in the Microsoft Access window.
application.OpenCurrentDatabase dbname[, exclusive]
The OpenCurrentDatabase method uses the following arguments.
Argument |
Description |
application |
The Application object. |
dbname |
A string expression that is the name of an existing database file, including the path name and the filename extension. If your network supports it, you can also specify a network path in the following form. |
\\Server\Share\Directory\Filename.mdb | |
exclusive |
Optional. An Integer value that specifies whether you want to open the database in exclusive mode. By default the database opens in shared mode. |
The OpenCurrentDatabase method enables you to open a Microsoft Access database from another application through OLE Automation. Once you have created an instance of Microsoft Access from another application, you must also create a new database or specify a particular database to open. This database opens in the Microsoft Access window.
If you have already opened a database and wish to open another database in the Microsoft Access window, you can use the CloseCurrentDatabase method to close the first database before opening another.
Set the exclusive argument to True (-1) to open the database in exclusive mode. If you omit this argument, the database will open in the shared mode.
If the database identified by dbname does not exist, an error occurs.
Note Don’t confuse the OpenCurrentDatabase method with the data access OpenDatabase method. The OpenCurrentDatabase method opens a database in the Microsoft Access window. The OpenDatabase method returns a Database object variable that represents a particular database, but doesn’t actually open that database in the Microsoft Access window.
CloseCurrentDatabase Method, NewCurrentDatabase Method.
The following example opens a Microsoft Access database from another application through OLE Automation, then opens a form in that database.
You can enter this code in a Visual Basic module in any application that can act as an OLE Automation controller. For example, you might run the following code from Microsoft Excel, Microsoft Visual Basic, or even Microsoft Access.
When the variable pointing to the Application object goes out of scope, the instance of Microsoft Access that it represents closes as well. Therefore, you should declare this variable at the module level.
' Include following in Declarations section of module. ' From Microsoft Excel, use "Dim appAccess As Object".appAccess As Access.Application DisplayForm() Dim strDB As String ' Initialize string to database path. strDB = "C:\Office95\Access\Samples\Northwind.mdb" ' Create new instance of Microsoft Access. Set appAccess = CreateObject("Access.Application.7") ' Open database in Microsoft Access window. appAccess.OpenCurrentDatabase strDB ' Open Orders form. appAccess.DoCmd.OpenForm "Orders"Sub
Note From some applications, such as Microsoft Visual Basic 4.0, you can include the New keyword when declaring the Application object variable. This keyword automatically creates a new instance of Microsoft Access, without requiring you to use the CreateObject function. Check your application’s documentation to determine whether it supports this syntax.