Datasheet
Designing Applications
43
But, apart from these, you'll find that with VBA you can do all that you could with macros and
lots more besides.
In early versions of Access, you also had to use macros if you wanted to create custom
menu bars or attach custom functionality to buttons on toolbars. However, from Access
97 onwards, both of these tasks are now achieved from the Customize… dialog box
available from Toolbars on the View menu.
Before we move on to the next chapter and completely discard macros in favor of VBA, let's just
take a look at the two things mentioned above where we still need macros.
Trapping Keystrokes Throughout an Application
Something you may want to do to make your application more user-friendly is to assign
frequently used actions to certain keystrokes. For example, you may want your application to
print the current record when your users hit Ctrl+P.
We have already seen that on a specific form you can implement a hotkey by using an
ampersand (&) in the caption for a control. That's what we did with the navigation button on
the Company Details form. However, if you want to implement a global keyboard shortcut –
one that is available throughout your application – you can do so by creating a special macro.
First create a new macro (click the down arrow next to the New Object button on the toolbar
and select Macro from the drop-down menu). You will need to save the macro with the name
Autokeys, as this is the name of the macro in which Access looks for keyboard shortcuts. To
display the Macro Name column, click on the Macro Names button on the toolbar. This button
toggles the column between visible and invisible. You can also do this by selecting
Macro Names from the View menu:
Then you specify the keystroke that you wish to instigate the required action in the
Macro Name column, and the action itself in the Action column. For example, the following
macro will cause the currently selected records to be printed whenever Ctrl+P is pressed: