Making Key Assignments

You can assign actions to key combinations by using a special key assignment macro. For example, suppose you frequently print the currently selected record. You can assign this task to a key combination, such as CTRL+R. Then, whenever you want to print the current record—no matter what form is active—you can just press CTRL+R. You can make as many key assignments as you want in your application.

When you press a key combination in an open database, Microsoft Access looks for a key assignment macro called AutoKeys. If it finds a macro that assigns actions to the key combination you pressed, Microsoft Access carries out the actions.

Tip   For even greater control over keystrokes, you can use a form or control’s KeyDown or KeyPress events to check which key the user pressed and respond appropriately. Use these events, in conjunction with the KeyPreview property, when you want to have different key assignments for each form or control in your application. For more information, see Chapter 6, “Responding to Events.”

Û To assign a set of actions to a key combination

  1. In the Database window, click the Macro tab and then click New.

  2. Display the Macro Name column by clicking Macro Names on the toolbar. In the Macro Name column, type the key combination you want from the table following this procedure.

    Important You use a special syntax (the Visual Basic SendKeys statement syntax) to specify the key combination. For a full description and examples of the SendKeys statement syntax, search the Help index for “SendKeys statement.”

  3. In the Action column, enter the actions you want this key combination to carry out.
  4. Save the macro with the name AutoKeys.

    Microsoft Access makes the key assignment immediately. When you press the key combination, Microsoft Access carries out the actions.

If you want the key assignment to be active only under certain conditions, enter an expression that specifies the conditions in the Condition column. To display the Condition column, click Conditions on the toolbar.

If you want to make several key assignments at once, create a macro for each key combination. Put the macros in a macro group named AutoKeys.

The following table gives the SendKeys syntax for some commonly used key combinations.

SendKeys syntax example Key combination
^A or ^4 CTRL+ any letter or number key
{F1} F1 (or other function key)
^{F1} CTRL+F1 (or other function key)
+{F1} SHIFT+F1 (or other function key)
%+{ F1} ALT+SHIFT+F1 (or other function key)

Note   If you assign a set of actions to a key combination that is already being used by Microsoft Access (for example, CTRL+C for Copy), the actions that you assign to this key combination in the key assignment macro replace the Microsoft Access key assignment in all Microsoft Access windows in the current database. If you have a custom menu command that you want associated with a key combination, you must define the key combination in the AutoKeys macro group. You can display text for the key combination with the menu command by specifying its ShortcutText property.

Example
Assigning the Print Current Record Command to a Key Combination

Northwind sales representatives frequently want to print the current record in either the Orders form or the ProductsPopup form. You can provide a convenient way for them to perform this task without having to go through the selection and print procedures each time: Assign these actions to the CTRL+R key combination.

Create a macro named AutoKeys. Type ^R (the Visual Basic SendKeys statement syntax for CTRL+R) in the Macro Name column, then select the RunMacro action and enter the PrintCurrentRecord macro as the action argument. (The Orders database includes the PrintCurrentRecord macro, which selects and then prints the current record. You can see how this macro works by opening it in Design view; then you can import the macro into your database if you want.)

If the user presses CTRL+R when the active window is not a form, such as while viewing a report in Print Preview, Microsoft Access displays an error message indicating that there is no current record. You can enter a condition to ensure that the key assignment is in effect only when a form is the active window.

The UtilityFunctions module in the Orders database includes a Visual Basic function named IsForm that you can use to check whether the active window is a form.

If the active window is a form, the IsForm function returns a value of -1, which means the condition is true. If the form isn’t open, the function returns a value of 0, which means the condition is false.

See Also   For information on how to write and use Visual Basic functions, see Chapter 2, “Introducing Visual Basic.”

Save the AutoKeys macro. Whenever a form is the active window, pressing CTRL+R now runs the PrintCurrentRecord macro, which selects and prints the current record. This key assignment is in effect every time you open the Orders database.