Application Object Example

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

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 a COM component. From Microsoft Excel, Visual Basic, or another application that acts as a COM 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 9.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