Understanding the Access Application Object
The Application object is the top-level object in the Microsoft® Access® object model. It provides properties and methods you can use to create and work with other Access objects. It also provides several built-in functions you can use to work with the objects in your database. In essence, the Application object serves as the gateway to all other Access objects.
Application-wide options are available through the Options dialog box and the Startup dialog box. The commands to open these dialog boxes are located on the Tools menu. You can use the Options dialog box to specify or determine application-wide settings, such as whether the status bar is displayed, the new database sort order, and the default record-locking settings. You use the Startup dialog box to specify or determine settings such as which form opens automatically when your database opens and your database application's title and icon. The following sections discuss how you can use Microsoft® Visual Basic® for Applications (VBA) to access all of these settings.
Working with the Options Dialog Box Settings
Use the Application object's SetOption and GetOption methods to specify or determine the settings in the Options dialog box. Both methods use a string argument that identifies the option you want to access. The SetOption method takes an additional argument representing the value you want to set. For example, the following code displays a message box that indicates whether datasheet gridlines are turned on:
MsgBox "Horizontal Gridlines On = " & _
CBool(GetOption("Default Gridlines Horizontal")) & vbCrLf _
& "Vertical Gridlines On = " & CBool(GetOption("Default Gridlines Vertical"))
The next example illustrates how you can use the SetOption method to specify a new default database folder:
SetOption "Default Database Directory", "C:\NewMDBs"
To see a list of all the string arguments used to access settings in the Options dialog box, search the Microsoft® Access Visual Basic Reference Help index for "options, setting," open the topic "Set Startup Properties and Options in Code," and then jump to the topic "Set Options from Visual Basic."
The value returned by the GetOption method and the value you pass to the SetOption method as the setting argument depend on the type of option you are using. The following table establishes some guidelines for Options dialog box settings.
If the option is | Then the value of the option is |
---|---|
A text box | A string or numeric value |
A check box | An integer that will be True (-1) (selected) or False (0) (not selected) |
An option button in an option group, or an item in a combo box or a list box | An integer corresponding to the item's position in the option group or list (starting with 0 for the first item, 1 for the second item, and so on) |
Note If you use the SetOption method to change a user's Options dialog box settings, be sure to restore those settings when your code is finished executing or when your application ends. Otherwise, the settings you specify will be applied to any database the user opens. Note that the settings in the Options dialog box are stored in the Microsoft® Windows® registry in the \HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Access\Settings subkey. As a result, changes to these settings will not persist if the database is run on a different machine.
Understanding Startup Properties
You use startup properties to customize how a database application appears when it is opened. You work with startup properties differently than you do the settings in the Options dialog box. Each option in the Startup dialog box has a corresponding Access property, but you won't find these properties in the Object Browser. In a new database, the startup properties do not exist until a user makes a change to the default settings in the Startup dialog box.
To set these properties programmatically for an .mdb-type database, you must first add each property to the Properties collection of the Database object. This is true whether you are using DAO or ADO. In other words, even without a reference to DAO, you still use the Properties collection of the Database object to work with these properties. In an .adp-type database, startup properties are stored in the Properties collection of the CurrentProject object.
In the following sample, the AddCustomProperty sample procedure is used to set the AppTitle property in an .mdb-type database. Note that if the property does not exist when the AddCustomProperty procedure is called, the property is created and appended to the Properties collection of the Database object.
Const TEXT_VALUE As Integer = 10
If AddCustomProperty("AppTitle", TEXT_VALUE, "MyDatabase") Then
' Property added to collection.
End If
Function AddCustomProperty(strName As String, _
varType As Variant, _
varValue As Variant) As Boolean
' The following generic object variables are required
' when there is no reference to the DAO 3.6 object library.
Dim objDatabase As Object
Dim objProperty As Object
Const PROP_NOT_FOUND_ERROR = 3270
Set objDatabase = CurrentDb
On Error GoTo AddProp_Err
objDatabase.Properties(strName) = varValue
AddCustomProperty = True
AddProp_End:
Exit Function
AddProp_Err:
If Err = PROP_NOT_FOUND_ERROR Then
Set prpProperty = objDatabase.CreateProperty(strName, varType, varValue)
objDatabase.Properties.Append objProperty
Resume
Else
AddCustomProperty = False
Resume AddProp_End
End If
End Function
Note Changes you make to any of the startup properties by using VBA will be available programmatically but will not take effect until the next time the database is opened.
For more information about setting startup properties, search the Microsoft® Access Visual Basic® Reference Help index for "startup options, setting," open the Help topic "Set Startup Properties and Options in Code," and then jump to "Set Startup Properties from Visual Basic."
See Also
Working with Microsoft Access Objects | Built-in Access Functions and Methods | Working with Reports, Forms, and Data Access Pages | Office Objects and Object Models