CloseCurrentDatabase Method

Applies To

Application Object.

Description

You can use the CloseCurrentDatabase method to close the current database from another application that has opened a database through OLE Automation. For example, you might use this method from Microsoft Excel to close the database currently open in the Microsoft Access window before opening another database.

Syntax

application.CloseCurrentDatabase

The CloseCurrentDatabase method uses the following argument.

Argument Description
application The Application object.


Remarks

The CloseCurrentDatabase method is useful when you have opened 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 an existing database to open. This database opens in the Microsoft Access window.

If you use the CloseCurrentDatabase method to close the database that is open in the current instance of Microsoft Access, you can then open a different database without having to create another instance of Microsoft Access.

Note Don’t confuse the CloseCurrentDatabase method with the data access Close method. The Close method closes a Database object variable, causing the variable to go out of scope, but it doesn’t actually close the database open in the Microsoft Access window. The CloseCurrentDatabase method closes the database that is open in the Microsoft Access window.

See Also

NewCurrentDatabase Method, OpenCurrentDatabase Method.

Example

The following example opens a Microsoft Access database from another application through OLE Automation, creates a new form and saves it, then closes the 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 or Microsoft Visual Basic.

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.


' Enter following in Declarations section of module.
' From Microsoft Excel, use "Dim appAccess As Object".appAccess As Access.Application
GetTableNames()
    Dim dbs As Database, frm As Form, strDB As String

    ' Initialize string to database path.
    strDB = "C:\MSOffice\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
    ' Create new form.
    Set frm = appAccess.CreateForm
    ' Save new form.
    appAccess.DoCmd.Save , "NewForm1"
    ' Close currently open database.
    appAccess.CloseCurrentDatabaseSub