Application Object

Description

The Application object refers to the active Microsoft Access application.

Remarks

The Application object contains all Microsoft Access objects and collections, including the Forms collection, the Reports collection, the Screen object, and the DoCmd object.

You can use the Application object to apply methods or property settings to the entire Microsoft Access application. For example, you can use the SetOption method of the Application object to set database options from Visual Basic. The following example shows how you can set the Show Status Bar option.


Application.SetOption "Show Status Bar", True

Microsoft Access is an OLE Automation server. You can manipulate Microsoft Access objects from another application that is an OLE Automation controller. To do this, you use the Application object.

For example, Microsoft Visual Basic is an OLE Automation controller. You can open a Microsoft Access database from Microsoft Visual Basic and work with its objects. From Microsoft Visual Basic, first create a reference to Microsoft Access. Then create a new instance of the Application object and point an object variable to it, as in the following example.


Dim appAccess As Access.ApplicationappAccess = CreateObject("Access.Application.7")

Once you have created a new instance of the Microsoft Access Application object, you can open a database or create a new database, using either the OpenCurrentDatabase method or the NewCurrentDatabase method. You can then set the properties of the Application object and call its methods.

Note If you are creating an instance of the Microsoft Access Application object from Microsoft Excel, you must declare the object variable as type Object, as in the following example.


Dim appAccess As ObjectappAccess = CreateObject("Access.Application.7")

You can also manipulate other Microsoft Access objects through the Application object. For example, using the OpenForm method of the Microsoft Access DoCmd object, you can open a Microsoft Access form from Microsoft Excel.


appAccess.DoCmd.OpenForm "OrderForm"



For more information on creating a reference and controlling objects by OLE Automation, see the documentation for the application that is acting as the OLE Automation controller.

Properties

DBEngine Property.

Methods

BuildCriteria Method; CloseCurrentDatabase Method; DefaultWorkspaceClone Method; Echo Method; GetOption, SetOption Methods; NewCurrentDatabase Method; OpenCurrentDatabase Method; Quit Method; RefreshTitleBar Method; Run Method.

See Also

CreateObject Function, DBEngine Object, DoCmd Object, Forms Collection, GetObject Function, Reports Collection, Screen Object.

Example

The following example prints some current property settings of the Application object, sets an option, and then quits the application, saving all objects.


Sub ApplicationInformation()
    ' Print name and type of current object.
    Debug.Print Application.CurrentObjectName
    Debug.Print Application.CurrentObjectType
    ' Set Show Hidden Objects option.
    Application.SetOption "Show Hidden Objects", True
    ' Quit Microsoft Access, saving all objects.
    Application.Quit acSaveYesSub



The next example shows how to use Microsoft Access as an OLE Automation server. From Microsoft Visual Basic or another application that acts as an OLE Automation controller, create a reference to Microsoft Access by clicking References on the Tools menu. Select the check box next to Microsoft Access For Windows 95. Then enter the following code in a Visual Basic module within that application.

This procedure creates a new instance of the Application object and opens a new database in the Microsoft Access Database window. It then creates a new form in that database.

Note that an instance of Microsoft Access will close when the variable pointing to the Application object goes out of scope. To prevent Microsoft Access from closing when the procedure has finished running, the variable representing the Application object is declared at the module level in the example below.


' Declare object variable in Declarations section of module.appAccess As Access.Application


NewAccessDB()
    ' Declare Database variable and Microsoft Access Form variable.
    Dim dbs As Database, frm As Access.Form

    ' Return instance of Application object.


    Set appAccess = CreateObject("Access.Application.7")
    ' Create new database in Microsoft Access window.
    appAccess.NewCurrentDatabase("Newdb.mdb")
    ' Create new Microsoft Access form in Design view.
    set frm = appAccess.CreateForm
    ' Restore form window.
    appAccess.DoCmd.RestoreSub