AutoKeys: One of Access's Most Well-Hidden Secrets
This is one of those features that most Access developers don't know that they don't know.
An AutoKeys macro allows you to create global shortcut keys in Microsoft Access that work universally on every form and report in your application.
How to Create the AutoKeys Macro
Once you know the secret incantation, creating the AutoKeys macro itself is surprisingly simple:
- Create a macro and name it "AutoKeys"
- Add a "Submacro" action
- Use the keyboard shortcut as the name of the submacro
- Add a "RunCode" action to the submacro
- Enter the name of a Public Function, including the parentheses
Now, strictly speaking, steps 4 and 5 can be replaced with different macro actions. But I loathe macros. The faster I can get out of the macro environment and back to VBA, the better.
Syntax for Keyboard Shortcuts
The syntax is available here.
However, since Microsoft will inevitably move or retire that help page at some point, I will reproduce the information here for posterity:
The following table shows some example keyboard shortcuts that you can enter in the submacro name box to make key assignments in an AutoKeys macro group. These keyboard shortcuts are a subset of the syntax that is used in the SendKeys statement in Microsoft Visual Basic.
Sample Use Cases
This is one of those features that you just have to try out as soon as you learn about it.
However, I've found there are actually relatively few uses that really make sense for a truly global shortcut key. Most shortcut keys make more sense in the context of a specific form, report, or control.
The other thing you have to be careful about is clobbering an existing shortcut key by defining an AutoKeys submacro. You may not use [F4] to toggle open a combo box, but one of your users might. If you assign that shortcut key to some other functionality, they would find that really annoying.
With that in mind, here are some sample ideas:
- {F1}: open custom help
- ^F: open a context-sensitive custom Find form
- ^+F: open a context-sensitive custom Filtering form
- ^+P: quick print the active form or report object
In addition to those user-focused ideas, you might consider some developer-focused shortcut keys. A guard clause in the called function could prevent the code from running if you're not in a development environment.
- ^+{F11}: go to the code behind of the active form or report object
- ^+Q: create a new query and switch it to SQL view
- etc.
Global Custom Help Command
In a few of my own applications, I've integrated user documentation that I created with DokuWiki with an {F1} AutoKeys submacro so that a user can click the [F1] key on their keyboard while viewing any form or report and go straight to the wiki page with information about that object.
Here's what that looks like:
The associated ShowHelp function is a bit complex (and unique to my environment), but it basically performs the following tasks:
Step 1. Creates a URL from the Following Elements
- A base URL
- The application's wiki namespace
- The form or report's Caption (falls back on Name)
- The active control's associated Label Caption (falls back on Name)
The form or report name is used as the page name. The code automatically appends "_form" or "_report" in case there are a form and report with the same name.
The control name (or its associated label or button caption) is appended to the URL as an anchor tag.
Here's an example from our Tax Collection software:
- Base URL:
https://grandjean.net/wiki/
- App namespace:
tax_coll
- Form caption:
ledger_menu_form
- Button caption:
ledger
https://grandjean.net/wiki/tax_coll/ledger_menu_form#ledger
The actual code gets a bit hairy, as I allow for explicit URL overrides via each object's .Tag
property. To do this, I use my Parse() function to extract text marked with the wiki
key.
For example, here's a Command Button Tag for a button that will open the URL https://grandjean.net/wiki/tax_coll/automatic_program_updates (but that will also slide 100% to the right and 100% down when its parent form resizes via my weResizer class):
Button Tag: wiki=automatic_program_updates; HSlide=1; VSlide=1
Here's the Parse function in action:
Debug.Print Parse("wiki=automatic_program_updates; HSlide=1; VSlide=1", "wiki")
automatic_program_updates
Step 2. Set Status Bar Text and Open URL in Browser
Opening the browser window, especially the first time, can sometimes take a few seconds.
For that reason, I turn on the hourglass and set a status bar message before attempting to open the URL.
After the URL has been opened, I clear the status bar message and turn off the hourglass.
Here's a simplified version of the code:
Public Function ShowHelp()
On Error Goto Err_ShowHelp
Dim FullUrl As String
FullUrl = BuildWikiUrl()
DoCmd.Hourglass True
SysCmd acSysCmdSetStatus, "Opening on-line help . . ."
Application.FollowHyperlink FullUrl
SysCmd acSysCmdClearStatus
Exit_ShowHelp:
DoCmd.Hourglass False
Exit Function
Err_ShowHelp:
Select Case Err.Number
Case 2465 'No current record. (This error occurs when pressing F1 in new records)
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
'LogError Err.Number, Err.Description, "ShowHelp"
End Select
Resume Exit_ShowHelp
End Function
Recommended Reading
Acknowledgements
- Cover image generated by DALL-E-3
- Cover image created with Microsoft Designer