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 Modules collection, the References 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 Status Bar check box under Show on the View tab of the Options dialog box.
Application.SetOption "Show Status Bar", True
Microsoft Access is an ActiveX component that supports Automation, formerly called OLE Automation. You can manipulate Microsoft Access objects from another application that also supports Automation. To do this, you use the Application object.
For example, Microsoft Visual Basic is an ActiveX component. You can open a Microsoft Access database from Visual Basic and work with its objects. From Visual Basic, first create a reference to the Microsoft Access 8.0 object library. Then create a new instance of the Application class and point an object variable to it, as in the following example:
Dim appAccess As New Access.Application
From applications that don't support the New keyword, you can create a new instance of the Application class by using the CreateObject function:
Dim appAccess As Object
Set appAccess = CreateObject("Access.Application.8")
Once you've created a new instance of the Application class, you can open a database or create a new database, by using either the OpenCurrentDatabase method or the NewCurrentDatabase method. You can then set the properties of the Application object and call its methods. When you return a reference to the DBEngine object by using the DBEngine property of the Application object, you can access all DAO objects and collections by using this reference.
You can also manipulate other Microsoft Access objects through the Application object. For example, by using the OpenForm method of the Microsoft Access DoCmd object, you can open a Microsoft Access form from Microsoft Excel:
appAccess.DoCmd.OpenForm "Orders"
For more information on creating a reference and controlling objects by using Automation, see the documentation for the application that's acting as the ActiveX component.
Properties
Application property, CodeContextObject property, CommandBar property, CurrentObjectType, CurrentObjectName properties, DBEngine property, MenuBar property, Parent property, ShortcutMenuBar property, UserControl property, Visible property.
Methods
AccessError method, AddToFavorites method, BuildCriteria method, CloseCurrentDatabase method, DefaultWorkspaceClone method, Echo method (Application object), FollowHyperlink method, GetOption, SetOption methods, NewCurrentDatabase method, OpenCurrentDatabase method, Quit method (Application object), RefreshDatabaseWindow method, RefreshTitleBar method, Run method, RunCommand method.
See Also
CreateObject function, DBEngine object ("DAO Language Reference"), DoCmd object, Forms collection, GetObject function, Module object, Modules collection, References collection, 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 Hidden Objects option under Show on View tab
' of Options dialog box.
Application.SetOption "Show Hidden Objects", True
' Quit Microsoft Access, saving all objects.
Application.Quit acSaveYes
End Sub
The next example shows how to use Microsoft Access as an ActiveX component. From Microsoft Excel, Visual Basic, or another application that acts as an ActiveX component, create a reference to Microsoft Access by clicking References on the Tools menu in the Module window. Select the check box next to Microsoft Access 8.0 Object Library. Then enter the following code in a Visual Basic module within that application and call the GetAccessData procedure.
The example passes a database name and report name to a procedure that creates a new instance of the Application class, opens the database, and prints the specified report.
' Declare object variable in declarations section of a module.
Dim appAccess As Access.Application
Sub GetAccessData()
Dim strDB As String
Dim strReportName As String
' Initialize string to database path.
strDB = "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"
' Initialize string to Report name.
strReportName = "Catalog"
PrintAccessReport strDB, strReportName
End Sub
Sub PrintAccessReport(strDB As String, strReportName As String)
' Return reference to Microsoft Access Application object.
Set appAccess = New Access.Application
' Open database in Microsoft Access.
appAccess.OpenCurrentDatabase strDB
' Print report.
appAccess.DoCmd.OpenReport strReportName
MsgBox "Click OK when " & strReportName & _
" is finished printing"
appAccess.CloseCurrentDatabase
Set appAccess = Nothing
End Sub