How to: Assign context menus to forms, reports and controls
Decide in one place which form, report, control, or kind of control shows which context menu of the ribbon model, instead of setting the Shortcut Menu Bar property object by object.
Requires WKL Forms Object Library 1.53 or later and an ACCDB file (assignments are written in Design view).
Solution overview
The Assignments page of the ribbon designer lists the forms and reports in the database. For
each one, it shows the object itself, every control that can have a context menu, and one row per
kind of control (all text boxes, all combo boxes, and so on), along with the menu each one has now
and the menu it gets after you click OK. The assignments are part of the ribbon model; after
OK, WKL Ribbon writes them to the Shortcut Menu Bar property of the objects
(WklRibbon.ApplyAssignments).
Open the page from Database Tools › Add-Ins › WKL Shortcut Menu Designer, from the page list of the ribbon designer, or from VBA:
CreateObject("WklForms.Ribbon").ShowAssignments
At run time, the menus themselves must exist: a ribbon from the model creates them when it loads,
and a database without such a ribbon calls EnsureMenus at startup.
Implementation
- On the Context menus page, create the menu, for example
ctxCustomer, with the entries the forms need. - On the Assignments page, select the form
frmCustomers. In the Text boxes row (the kind of control), choosectxCustomer: every text box on the form gets it. In the row for the combo boxcboCity, choosectxCustomeras well. - Select the report
rptCustomersand choosectxCustomerin the row for the report itself. - Click OK. The After OK column showed what's written; the forms and reports are opened hidden in Design view and saved where something changes.
- In a database without a ribbon from the model, create the menus at startup:
Private Sub Form_Load()
' The context menus are temporary command bars: Access forgets them when it closes.
CreateObject("WklForms.Ribbon").EnsureMenus
End Sub
Remarks
- Which assignment wins. The row for a named control wins over the row for its kind. Choosing no menu for a place the model had assigned removes the menu there. A context menu the model doesn't know (created by the application's own code) is never removed; it's only replaced where the model assigns another menu.
- Open objects are skipped. A form or report that's open when the designer closes is left unchanged, and a message names it. Close it and open the designer again, or call
CreateObject("WklForms.Ribbon").ApplyAssignments. - ACCDE files. An ACCDE file has no Design view: the assignments are written to the ACCDB file, and an ACCDE file made from it keeps them. In an ACCDE file,
ApplyAssignmentsraiseswkeNotAvailable. - Build tool. With
ribbonModelin access-app.json, the build writes the assignments after it loads the forms and reports; an assignment that names a form or report the build doesn't create stops the build. - Images. Entries show Office images (
imageMso) and images from the database's image gallery (drawn at 16 × 16 pixels on the menu color).