How to: Create a Shortcut Menu for a Form, Form Control, or Report

Today’s guest blogger is Edwin Blancovitch. Edwin is president of Advanced Developers.net, creators of Easy Payroll, a software package to manage your human resources, payroll, scheduling, time and attendance needs.

When you're designing a form or report, you may want to provide a method for a user to easily use a command that applies only to the correct context. One way to do this is to create a custom shortcut menu and apply it to a form report, or control. The shortcut menu appears when the user right-clicks the object to which the shortcut menu is applied.

In earlier versions of Access, you could use the Customize dialog box to create custom shortcut menus. In Microsoft Office Access 2007, you must use Visual Basic for Applications (VBA) code to create a shortcut menu. This article shows you how to create a shortcut menu using VBA.

To create a shortcut menu, you first have to create a CommandBar object. The CommandBar object represents the shortcut menu. Then, you use the Add method to create CommandBarControl objects. Each time you create a CommandBarControl object, a command is added to the shortcut menu.

Example 1: A Simple Shortcut Menu

The following example creates a shortcut menu named SimpleShortcutMenu that contains two commands, Remove Filter/Sort and Filter by Selection.

Note To use the following examples, you must set a reference to the Microsoft Office 12.0 Object Library. See Set References to Type Libraries for more information about setting references.

Sub CreateSimpleShortcutMenu()
    Dim cmbShortcutMenu As Office.CommandBar
    
    ' Create a shortcut menu named "SimpleShortcutMenu".
    Set cmbShortcutMenu = CommandBars.Add("SimpleShortcutMenu", _
                        msoBarPopup, False, False)
                        
    ' Add the Remove Filter/Sort command.
    cmbShortcutMenu.Controls.Add Type:=msoControlButton, Id:=605
    
    ' Add the Filter By Selection command.
    cmbShortcutMenu.Controls.Add Type:=msoControlButton, Id:=640
    
    Set cmbShortcutMenu = Nothing
End Sub

Once you've run the code, the shortcut menu is saved as part of the database. You don't have to run the same code to recreate the shortcut menu each time you open the database.

To assign the shortcut menu to a form, form control, or report, set Shortcut Menu property of the object to Yes and set the Shortcut Menu Bar property of the object to the name of the shortcut menu. For this example, set the Shortcut Menu Bar property to SimpleShortcutMenu.

Note In some instances, you may want to create a temporary CommandBar. To do this, set the Temporary argument of the Add method to True. When you do this, the CommandBar is deleted when Access is closed.

Example 2: A Shortcut Menu With Grouping

The following example creates a shortcut menu named cmdFormFiltering that contains commands that are useful to use with Continuous forms. In this example, the BeginGroup property is used on several controls to group controls visually.

Sub CreateShortcutMenuWithGroups()
    Dim cmbRightClick As Office.CommandBar
    
    ' Create the shortcut menu.
    Set cmbRightClick = CommandBars.Add("cmdFormFiltering", _
                      msoBarPopup, False, False)
    
    With cmbRightClick
        ' Add the Find command.
        .Controls.Add msoControlButton, 141
        
        ' Start a new grouping and add the Sort Ascending command.
        .Controls.Add(msoControlButton, 210).BeginGroup = True
        
        ' Add the Sort Descending command.
        .Controls.Add msoControlButton, 211
        
        ' Start a new grouping and add the Remove Filer/Sort command.
        .Controls.Add(msoControlButton, 605).BeginGroup = True
        
        ' Add the Filter by Selection command.
        .Controls.Add msoControlButton, 640
        
        ' Add the Filter Excluding Selection command.
        .Controls.Add msoControlButton, 3017
        
        ' Add the Between... command.
        .Controls.Add msoControlButton, 10062
    End With
End Sub

Example 3: A Shortcut Menu for Reports

The following example creates a shortcut menu named cmdReportRightClick that contains commands that are useful to use with a report. This example illustrates how to change the Caption property of each control as they're added to the shortcut menu.

 
Sub CreateReportShortcutMenu()
    Dim cmbRightClick As Office.CommandBar
    Dim cmbControl As Office.CommandBarControl

    Set cmbRightClick = CommandBars.Add("cmdReportsRightClick", _
                                        msoBarPopup, False, False)

    With cmbRightClick
        
        ' Add the Print command.
        Set cmbControl = .Controls.Add(msoControlButton, 2521)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Quick Print"
        
        ' Add the Print command.
        Set cmbControl = .Controls.Add(msoControlButton, 15948)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Select Pages"
        
        ' Add the Page Setup... command.
        Set cmbControl = .Controls.Add(msoControlButton, 247)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Page Setup"
        
        ' Add the Mail Recipient (as Attachment)... command.
        Set cmbControl = .Controls.Add(msoControlButton, 2188)
        ' Start a new group.
        cmbControl.BeginGroup = True
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Email Report as an Attachment"
        
        ' Add the PDF or XPS command.
        Set cmbControl = .Controls.Add(msoControlButton, 12499)
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Save as PDF/XPS"
        
        ' Add the Close command.
        Set cmbControl = .Controls.Add(msoControlButton, 923)
        ' Start a new group.
        cmbControl.BeginGroup = True
        ' Change the caption displayed for the control.
        cmbControl.Caption = "Close Report"
    End With
    
    Set cmbControl = Nothing
    Set cmbRightClick = Nothing
End Sub

 

Displaying the Shortcut Menu

By default, a shortcut menu is displayed when the user right-clicks the object that the shortcut menu is assigned to. The problem is, many users do not know that right-clicking their mouse can display a menu.

You can use the ShowPopup method to display a shortcut menu when the user clicks on an object. The following example shows how to display the cmdFormFiltering shortcut menu when the user clicks the Command0 button.

Private Sub Command0_Click()
    
    ' Display the shortcut menu when the user clicks the button.
    Application.CommandBars("cmdFormFiltering").ShowPopup
    
End Sub

How to Discover the IDs to Use

In the previous examples, a numeric ID was used to specify the control to the Add method. How do you find the ID to use for the control that you want to add? Access doesn’t provide a built-in method that you can use to find right ID to use.

The following example shows how to find the right ID to use. The CreateCommandBarsWithIDs function uses cbShowButtonFaceIds function to create four CommandBars, each with 500 controls.

The CommandBars are displayed on the Add-Ins tab of the Ribbon. The number displayed beside each icon is the ID to use when adding the control to a shortcut menu.

Function CreateCommandBarsWithIDs()
    Call cbShowButtonFaceIds("CmdIDs_01", 1, 500)
    Call cbShowButtonFaceIds("CmdIDs_02", 501, 1000)
    Call cbShowButtonFaceIds("CmdIDs_03", 1001, 1500)
    Call cbShowButtonFaceIds("CmdIDs_04", 1501, 2000)
End Function

Function cbShowButtonFaceIds(strName As String, _
                             lngIDStart As Long, _
                             lngIDStop As Long)

    Dim cbrNewToolBar As CommandBar
    Dim cmdNewButton As CommandBarButton
    Dim intCntr As Integer

    On Error Resume Next

    ' Delete the CommandBar if it already exists.
    Application.CommandBars(strName).Delete

    ' Create the CommandBar.
    Set cbrNewToolBar = Application.CommandBars.Add( _
                      Name:=strName, temporary:=True)

    ' Loop through the IDs.
    For intCntr = lngIDStart To lngIDStop
        ' Create a new button for each ID.
        Set cmdNewButton = cbrNewToolBar.Controls.Add( _
                         Type:=msoControlButton)

        With cmdNewButton
            .FaceId = intCntr
            .TooltipText = "Faceid= " & intCntr
            .Caption = intCntr
            .Style = msoButtonIconAndCaptionBelow
        End With

        ' This takes awhile to run. Display a count
        ' to indicate progress.
        Debug.Print intCntr & " of " & lngIDStop
    Next intCntr

    ' Display the new CommandBar.
    With cbrNewToolBar
        .Width = 600
        .Left = 100
        .Top = 200
        .Visible = True
    End With

    Set cbrNewToolBar = Nothing
    Set cmdNewButton = Nothing
End Function

Office Blogs Comments

Comments: (3) Collapse

  • A very useful article. Forced by the poor documentation on this functionality in the Access 2007 Help file and from MDSN, I have been using a workaround, that in a way is both easier and more flexible, and embeds the custom popups in the actual database. You need Access 2003 installed on the development machine. As any serious Access developer retains at least the previous version of Access on his machine, this should not be an issue. In an Access 2003 MDB create your library of custom popup commandbars the easy way using the Customize dialog. In your Access 2007 MDB or ACCDB, from the RibbonBar, use the Get External Data commands to invoke the Import Objects dialog (to import from the Access 2003 database), click Options>>, check "Menus and Toolbars", and click OK. All you're custom popups are now available for assignment using the form properties dialog or VBA.

  • How does one delete a custom shortcut menu created with this code?  It must be so easy I'm missing it. Thanks. BTW, nice coding.

  • Ans.  In code, the easiest is to just delete the custom command bar.  If the command bar name is SimpleShortCutMenu, then use:

    CommandBars.Item("SimpleShortcutMenu").Delete

    One can also delete individual control items in the command bar, but why bother when it is easier to delete and recreate. To delete individual control items use something like:

    CommandBars.Item("SimpleShortcutMenu").Controls.Item(3).Delete

    where 3 is the third control command in the shortcut menu.

    or

    CommandBars.Item("SimpleShortcutMenu").Controls.Item("Find").Delete        

    where "Find" is the name of the control command

Comments

Comments: (loading) Collapse