Microsoft Access VBA Programming for the Absolute Beginner

In the previous chapter, you learned how to manipulate command bars (menus and toolbars) using VBA. That changed how you could interact with Access. In this chapter, you will see how to change other environmental settings by using VBA. We are going to look at some simple startup procedures as well as changing and restoring default information. The second half of the chapter contains tables that you can use as a handy reference.

The Startup Properties

On several occasions you have already encountered the database startup properties. You can access them from the Database window by selecting Tools | Startup. You will see the dialog box shown in Figure 15-1.

Figure 15-1: The database startup properties dialog box

Interestingly, each item in this box has a VBA equivalent. Table 15-1 lists the startup properties that correspond to the Startup dialog box interface items and the type of values they are.

Table 15-1: VBA Equivalents of Startup Properties

Property

VBA Equivalent

Data Type

Application Title

AppTitle

String

Application Icon

AppIcon

String (Image name)

Display Form

StartupForm

String (Form name)

Display Database Window

StartupShowDBWindow

Boolean

Display Status Bar

StartupShowStatusBar

Boolean

Menu Bar

StartupMenuBar

Menu bar name

Shortcut Menu Bar

StartupShortcutMenuBar

Shortcut menu bar name

Allow Full Menus

AllowFullMenus

Boolean

Allow Default Shortcut Menus

AllowShortcutMenus

Boolean

Allow Built-in Toolbars

AllowBuiltinToolBars

Boolean

Allow Toolbar/Menu Changes

AllowToolBarChanges

Boolean

Allow viewing of code after an error (Note: While not specifically listed in the dialog box, this shuts off controls to access the code.)

AllowBreakIntoCode

Boolean

Use Access Special Keys

AllowSpecialKeys

Boolean

The following code shows an example of changing some startup properties using the current database as the object.

Public Sub startupProperties() Dim myDatabase As Object Set myDatabase = CurrentDb myDatabase.Properties("AllowFullMenus") = True myDatabase.Properties("Allowtoolbarchanges") = True End Sub

If you try to run this code and you get an error message, open the Startup dialog box and click OK. This lets Access populate the properties of the Application and Database objects as needed.

When you run the code, nothing seems to happen. However, try changing the two Boolean values to False as follows:

Public Sub startupProperties() Dim myDatabase As Object Set myDatabase = CurrentDb myDatabase.Properties("AllowFullMenus") = False myDatabase.Properties("Allowtoolbarchanges") = False End Sub

If you now open the dialog box, notice that those fields are toggled off, as shown in Figure 15-2.

Figure 15-2: The Startup dialog box with fields clicked off

You can reset any values you set with the following code:

Public Sub startupProperties() Dim myDatabase As Object Set myDatabase = CurrentDb With myDatabase .Properties.Delete "AllowFullMenus" .Properties.Delete "AllowToolBarChanges" End With Application.RefreshTitleBar End Sub

Категории