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.
Private Sub Command0_Click() ' Display the shortcut menu when the user clicks the button. Application.CommandBars("cmdFormFiltering").ShowPo pup End Sub How to Discover the IDs to Use
In the previous examples,
Office Home And Business 2010 Key, 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.
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,
Windows 7 Home Premium Key, payroll, scheduling, time and attendance needs.
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.
Example 1: A Simple Shortcut Menu
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.
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 = NothingEnd Sub
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 WithEnd 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.
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.
Example 2: A Shortcut Menu With Grouping
Displaying the 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,
Cheap Office 2010, _ 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 = NothingEnd Function <div
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,
Office 2007 Professional Plus Key, or control. The shortcut menu appears when the user right-clicks the object to which the shortcut menu is applied.
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.
Note In some instances,
Windows 7 Ultimate Product Key, 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.
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.
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 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 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 = NothingEnd 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.
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.