Build a command bar
In this tutorial, you put a command bar in the header of a customer form: a primary command, a split button for saving, and a menu button for the view. You set up all three in the designer without code, and then add the event procedures that respond to them.
Prerequisites
- Access in Microsoft 365 or the Access Runtime, 32-bit or 64-bit, with the WKL Forms Object Library 1.27 or later installed and registered for the current Windows user (How to: Install the released package).
- A form bound to a table, for example
frmCustomersbased ontblCustomers. - Every WKL control opens its designer the same way: in Design view, open the property sheet (F4), select the Other tab, and click
[...]in the Custom row (How to: Open the designer of a control).
Steps
1. Insert three buttons in the form header
Open frmCustomers in Design view, show the form header, and insert WKL Button three times
(Form Design › Controls › ActiveX Controls). On the Other tab of the property
sheet, name the buttons btnNew, btnSave, and btnView, and then set the background color of
the form header.
2. Set up the primary command
Select btnNew, click [...] in the Custom row of the property sheet, and on the Button
page set: kind Command button, caption &New, symbol E710, and style Primary command. On
the Appearance page, set the background to the color of the form header.
3. Set up the split button
For btnSave: kind Split button, caption &Save, symbol E74E. On the Menu page, enter
two rows:
| Key | Caption |
|---|---|
| Save as &PDF | |
| xlsx | Save as &Excel workbook |
4. Set up the menu button
For btnView: kind Menu button, caption &View, style Subtle. On the Menu page,
enter:
| Key | Caption | Group | Check mark |
|---|---|---|---|
| name | By &Name | sort | ✓ |
| city | By &City | sort |
5. Respond to the events
Open the form's module and add:
Private Sub btnNew_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub btnSave_Click()
If Me.Dirty Then Me.Dirty = False
End Sub
Private Sub btnSave_MenuItemClick(ByVal Key As String)
Select Case Key
Case "pdf": DoCmd.OutputTo acOutputForm, Me.Name, acFormatPDF
Case "xlsx": DoCmd.OutputTo acOutputForm, Me.Name, acFormatXLSX
End Select
End Sub
Private Sub btnView_MenuItemClick(ByVal Key As String)
Me.OrderBy = IIf(Key = "name", "Name", "City")
Me.OrderByOn = True
End Sub
When the user chooses “By City” in the menu, the menu checks that entry and clears the check mark
from “By Name” before MenuItemClick occurs, because both entries belong to the “sort” option
group.
Expected result
In Form view, the header shows the three buttons with the Office look. New adds a record, the main part of Save saves it, its arrow opens the two export options, and View opens a menu with the checked sort order. Spacebar or Enter presses the button that has the focus, and Alt+Down Arrow or F4 opens a menu.
Next steps
- How to: Fill a menu just before it opens, for entries that depend on the current record.