Online Book Reader

Home Category

Access Cookbook - Ken Getz [181]

By Root 2039 0
application, but you'd like the shortcut keys to change based on the currently active form. Is there an easy way to create context-sensitive keyboard shortcuts in Access?

Solution


The SetOption method of the Application object allows you to change global database options programmatically. This solution shows you how to combine this functionality with the Activate and Deactivate event properties of your forms to create custom key shortcut macros for each form of your application.

For an example of key assignments that depend on the active form, open 09-01.MDB. This sample database contains information on units, assemblies that make up parts, and parts that make up assemblies. Open the frmUnit form in form view. At any time, you can press Ctrl-D to "drill down" to the next level of detail or Ctrl-R to revert to the previous level of detail. When you press Ctrl-D on frmUnit, frmAssembly is loaded; if you press Ctrl-D from frmAssembly, frmPart is loaded (see Figure 9-1). If you press Ctrl-D a third time while frmPart has the focus, nothing happens. Thus, the behavior of Ctrl-D changes based on its context. The Ctrl-R keyboard macro is similarly context-sensitive.

Figure 9-1. The sample database after pressing Ctrl-D twice

TIP

To keep the example simple, we have not added the additional macro code necessary to keep the forms synchronized. You must manually use Ctrl-R to return to the previous level/form, then navigate to the desired record, and then use Ctrl-D to drill down if you wish to keep the forms synchronized.

To add context-sensitive AutoKeys macros to your own application, follow these steps:

Create a key assignment macro for each form in your application (you can use the same macro for more than one form if you like). Follow all the design rules for an AutoKeys macro, but give your macro a unique name when you are done. In the sample application, for instance, the three key assignment macros are called mcrUnitAutoKeys, mcrAssemblyAutoKeys, and mcrPartAutoKeys, so that the macro name reminds you of its function. Table 9-1 shows the settings for the mcrUnitAutoKeys macro.

You'll probably want to add comments to your macro to make it easier to understand and maintain, as illustrated in Figure 9-2.

Table 9-1. Settings for the mcrUnitAutoKeys macro

Macro name

Action

Argument

Value

^D

OpenForm

Form Name

frmAssembly

View

Form

Where Condition

[UnitNumber]=[Forms]![frmUnit]![UnitNumber]

Data Mode

Edit

Window Mode

Normal

^R

Close

Object Type

Form

Object Name

frmUnit

Figure 9-2. The mcrUnitAutoKeys macro

Import the basOptions module from 09-01.MDB into your own database.

Add a RunCode action to your AutoExec macro (or create a new macro named AutoExec containing this one action). Set the action's Function Name argument to:

=acbStoreOriginalAutoKeys( )

In the OnActivate event property of each of your forms, add a call to the acbSetAutoKeys function. This function takes a single argument, the name of the key assignment macro to use while that form is active. For example, on the frmUnit form in the sample application, this property is set to:

=acbSetAutokeys("mcrUnitAutokeys")

In the OnClose event of the last form to be closed in your application (typically, your main switchboard form), add a call to the acbRestoreOriginalAutokeys function. If there is more than one possible last form in your application, you'll need to add this function call to every possible last form. acbRestoreOriginalAutokeys takes no arguments. Figure 9-3 shows these calls in the sample application.

Figure 9-3. Event properties for frmUnit

Discussion


The special built-in Application object refers to your entire Access application. The GetOption method of this object lets you read the options stored under Tools Options, Tools Startup, and additional options that are available only programmatically. The Key Assignment Macro option, which was originally part of the View Options dialog in Access 2.0, is no longer available from the Access user interface, but fortunately

Return Main Page Previous Page Next Page

®Online Book Reader