Table of contents
XML
TOC
折叠目录
展开目录

为窗体、 窗体控件或报表创建快捷菜单Create a Shortcut Menu for a Form, Form Control, or Report

office 365 dev account|上次更新日期: 2018/4/3
|
1 参与者

示例提供的代码:Edwin Blancovitch,高级 Developers.netSample code provided by: Edwin Blancovitch,Advanced Developers.net

当您在设计窗体或报表时,您可以提供一种方法让用户方便地使用仅适用于当前上下文的命令。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 current 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.

在早期版本的Access中,您可以使用自定义对话框中创建自定义快捷菜单。In earlier versions of Access, you could use the Customize dialog box to create custom shortcut menus.在访问 2013年必须使用 Visual Basic for Applications (VBA) 代码来创建一个快捷菜单。In Access 2013, you must use Visual Basic for Applications (VBA) code to create a shortcut menu.本文介绍了如何创建使用 VBA 的快捷菜单。This article describes you how to create a shortcut menu using VBA.

若要创建一个快捷菜单,首先需要创建一个命令栏对象。To create an shortcut menu, you first have to create a CommandBar object.命令栏对象表示的快捷菜单。The CommandBar object represents the shortcut menu.然后,您可以使用Add方法创建命令栏控件对象。Then, you use the Add method to create CommandBarControl objects.创建命令栏控件对象,每次一个命令添加到快捷菜单。Each time that you create a CommandBarControl object, a command is added to the shortcut menu.下面的示例创建一个名为SimpleShortcutMenu ,其中包含两个命令,取消筛选/排序按选定内容筛选的快捷菜单。The following example creates a shortcut menu named SimpleShortcutMenu that contains two commands, Remove Filter/Sort and Filter by Selection.

请注意 若要使用下面的示例中,您必须设置对Microsoft Office 于 15.0 对象库的引用。Note To use the following examples, you must set a reference to the Microsoft Office 15.0 Object Library.有关如何设置引用的详细信息,请参阅设置对类型库的引用SeeSet References to Type Libraries for more information about how to set references.

Sub CreateSimpleShortcutMenu() 
    Dim cmbShortcutMenu As Office.CommandBar 

    ' Create a shortcut menu named "SimpleShortcutMenu. 
    Set cmbShortcutMenu = CommandBars.Add("SimpleShortcutMenu", msoBarPopup, False, True) 

    ' 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 re-create the shortcut menu every time that you open the database.分配此快捷方式菜单的窗体、 窗体控件或报表中,将该对象的快捷菜单属性设置为,并将该对象的快捷菜单栏属性设置为快捷菜单的名称。To assign the shortcut menu to a form, form control, or report, set the 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.对于此示例,将快捷菜单栏属性设置为SimpleShortcutMenuFor this example, set the Shortcut Menu Bar property to SimpleShortcutMenu.下面的示例创建一个名为cmdFormFiltering ,其中包含命令的快捷菜单,可使用的连续窗体。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, True) 

    With cmbRightClick 
        ' Add the Find command. 
        .Controls.Add msoControlButton, 141, , , True 

        ' Start a new grouping and add the Sort Ascending command. 
        .Controls.Add(msoControlButton, 210, , , True).BeginGroup = True 

        ' Add the Sort Descending command. 
        .Controls.Add msoControlButton, 211, , , True 

        ' Start a new grouping and add the Remove Filer/Sort command. 
        .Controls.Add(msoControlButton, 605, , , True).BeginGroup = True 

        ' Add the Filter by Selection command. 
        .Controls.Add msoControlButton, 640, , , True 

        ' Add the Filter Excluding Selection command. 
        .Controls.Add msoControlButton, 3017, , , True 

        ' Add the Between... command. 
        .Controls.Add msoControlButton, 10062, , , True 
    End With 

Set cmbRightClick = Nothing 
End Sub

下面的示例创建一个名为cmdReportRightClick ,其中包含命令的快捷菜单,可用于报表。The following example creates a shortcut menu named cmdReportRightClick that contains commands that are useful to use with a report.此示例演示如何更改每个控件的标题属性,因为它们会添加到快捷菜单。This example shows 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 

   ' Create the shortcut menu. 
    Set cmbRightClick = CommandBars.Add("cmdReportRightClick", msoBarPopup, False, True) 

    With cmbRightClick 

        ' Add the Print command. 
        Set cmbControl = .Controls.Add(msoControlButton, 2521, , , True) 
        ' Change the caption displayed for the control. 
        cmbControl.Caption = "Quick Print" 

        ' Add the Print command. 
        Set cmbControl = .Controls.Add(msoControlButton, 15948, , , True) 
        ' Change the caption displayed for the control. 
        cmbControl.Caption = "Select Pages" 

        ' Add the Page Setup... command. 
        Set cmbControl = .Controls.Add(msoControlButton, 247, , , True) 
        ' Change the caption displayed for the control. 
        cmbControl.Caption = "Page Setup" 

        ' Add the Mail Recipient (as Attachment)... command. 
        Set cmbControl = .Controls.Add(msoControlButton, 2188, , , True) 
        ' 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, , , True) 
        ' Change the caption displayed for the control. 
        cmbControl.Caption = "Save as PDF/XPS" 

        ' Add the Close command. 
        Set cmbControl = .Controls.Add(msoControlButton, 923, , , True) 
        ' 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

关于参与者About the Contributors

Edwin Blancovitch 是总裁高级 Developers.net简单的工资,来管理人力资源、 工资、 时间安排、 时间和出勤的需要一个软件程序包的创建者。Edwin Blancovitch is president of Advanced Developers.net, creators of Easy Payroll, a software package to manage your human resources, payroll, scheduling, time and attendance needs.

© 2018 Microsoft