Chapter 29: Customizing Access Ribbons
1027
Step 2: Write the callback routines
Before writing any callback code for Access ribbon controls, you must reference the Microsoft
Office 14.0 Object Library in the References dialog box (select Tools ➪ References, and select the
check box next to Microsoft Office 14.0 Object Library). Otherwise, the VBA interpreter will have
no idea how to handle references to ribbon controls.
As we described earlier in this chapter, callback routines are similar to event procedures, but do
not directly respond to control events. Each type of callback routine has a specific “signature” that
must be followed in order for the ribbon processor to locate and use the callback. For instance, the
prototype onAction callback signature for a button control is
Public Sub OnAction(control as IRibbonControl)
The prototype onAction callback for a check box is
Public Sub OnAction(control As IRibbonControl, _
pressed As Boolean)
Even though these callbacks support the same onAction control attribute, because the controls
are different, the signatures are different. Clicking a button is just that — click once, and the action
is done. In the case of a check box, a click either selects (pressed = True) or deselects
(pressed = False) the control. Therefore, an additional parameter is required for check boxes.
These procedures are just prototypes and do not apply to any particular control on a ribbon. In
practice, the callback procedure for a control is usually named after the control to distinguish it
from callback procedures for other controls. For example, the actual callback for the ViewProducts
button described in the “Using VBA callbacks” section, earlier in this chapter, might be:
Public Sub OpenProductsForm(control As IRibbonControl)
‘Called from ViewProducts ribbon button
DoCmd.OpenForm “frmProducts”
End Sub
Notice that this procedure’s declaration matches the prototype for a button control’s onAction
callback procedure. Although not required, this procedure even contains a comment that identifies
the ribbon control that calls the routine.
Callback routines must be declared with the Public attribute, or they can’t be seen by the Access
ribbon process.
The name you apply to callback routines is entirely your choice, as long as the procedure’s declara-
tion matches the control’s onAction signature. Obviously, the procedure’s name must match the
value you assign to the control’s onAction attribute, and documenting the procedure’s relation-
ship to a ribbon control is very helpful when it comes time to modify the ribbon or the callback.
The complete callback procedure for a simple button might be
Public Sub OnAction(control As IRibbonControl)
DoCmd.OpenForm “frmMyForm”, , , , acNormal
End Sub