适用于:Access 2013
在设计窗体或报表,您可能需要为用户提供一种方法,以便轻松地使用仅适用于当前上下文的命令。 执行此操作的一种方式是创建自定义快捷菜单,并将其应用于窗体报表或控件。 当用户右键单击快捷菜单所应用的对象时,将会显示快捷菜单。
在 Access 的早期版本中,可以使用自定义对话框中来创建自定义快捷菜单。 在 Access 2013 中,您必须使用 Visual Basic for Applications (VBA) 代码来创建快捷菜单。 本文介绍如何使用 VBA 创建快捷菜单。
若要创建快捷菜单,首先必须创建 CommandBar 对象。 CommandBar 对象表示快捷菜单。 然后,使用 Add 方法创建 CommandBarControl 对象。 每次创建 CommandBarControl 对象时,会向快捷菜单中添加一个命令。
下面的示例创建一个名为 SimpleShortcutMenu 的快捷菜单,该快捷菜单包含两条命令“取消筛选/排序”和“按选定内容筛选”。
注意
若要使用以下示例,必须设置对 Microsoft Office 15.0 对象库的引用。 有关如何设置引用的详细信息,请参阅设置对类型库的引用。
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
运行代码之后,快捷菜单将保存为数据库的一部分。 您不必在每次打开数据库时运行相同代码来重新创建快捷菜单。
若要为窗体、窗体控件或报表分配该快捷菜单,请将对象的“快捷菜单”属性设置为“是”,将对象的“快捷菜单栏”属性设置为该快捷菜单的名称。 对于此示例,请将“快捷菜单栏”属性设置为 SimpleShortcutMenu。
下面的示例创建一个名为 cmdReportRightClick 的快捷菜单,该快捷菜单包含在与连续窗体结合使用时很有用的命令。 在此示例中,若干控件上使用了用于直观地将控件分组的 BeginGroup 属性。
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 的快捷菜单,该快捷菜单包含在与报表结合使用时很有用的命令。 此示例演示在将这些命令添加到快捷菜单时,如何更改每个控件的 Caption 属性。
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
关于参与者
Edwin Blancovitch 提供的示例代码。Edwin Blancovitch 是 Advanced Developers.net 的总裁,Easy Payroll 的创建者,Easy Payroll 是一个用于管理人力资源、工资、日程安排、时间和考勤需求的软件包。
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。