The goal is to create menus which can be utilized with certain controls on an MS Access form and to be able to right click on a that control, for example on a listbox and a relevant context specific menu popup with options, which if clicked, would trigger a predefined subroutine or function.
目标是创建可以与MS Access表单上的某些控件一起使用的菜单,并且能够右键单击该控件,例如在列表框和具有选项的相关上下文特定菜单弹出窗口,如果单击,将会触发预定义的子程序或功能。
What is the best method to accomplish this programmatically?
以编程方式完成此操作的最佳方法是什么?
I am using MS Access 2003 and would like to do this using VBA.
我正在使用MS Access 2003,并希望使用VBA执行此操作。
First create an _MouseUp
event to execute on the respective control looking to see if the right mouse button was clicked and if so, call the .ShowPopup
method.
首先创建一个_MouseUp事件,在相应的控件上执行,查看是否单击了鼠标右键,如果是,则调用.ShowPopup方法。
Of course this assumes the
当然这假定了
Private Sub MyListControlName_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Long, ByVal Y As Long)
' Call the SetUpContextMenu function to ensure it is setup with most current context
' Note: This really only needs to be setup once for this example since nothing is
' changed contextually here, but it could be further expanded to accomplish this
SetUpContextMenu
' See if the right mouse button was clicked
If Button = acRightButton Then
CommandBars("MyListControlContextMenu").ShowPopup
End If
End Sub
Since at this point the Command Bar MyListControlContextMenu
is undefined, I define the Menu in a separate module as follows:
由于此时命令栏MyListControlContextMenu未定义,我在单独的模块中定义Menu,如下所示:
Public Sub SetUpContextMenu()
' Note: This requires a reference to Microsoft Office Object Library
Dim combo As CommandBarComboBox
' Since it may have been defined in the past, it should be deleted,
' or if it has not been defined in the past, the error should be ignored
On Error Resume Next
CommandBars("MyListControlContextMenu").Delete
On Error GoTo 0
' Make this menu a popup menu
With CommandBars.Add(Name:="MyListControlContextMenu", Position:=msoBarPopup)
' Provide the user the ability to input text using the msoControlEdit type
Set combo = .Controls.Add(Type:=msoControlEdit)
combo.Caption = "Lookup Text:" ' Add a label the user will see
combo.OnAction = "getText" ' Add the name of a function to call
' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.BeginGroup = True ' Add a line to separate above group
combo.Caption = "Lookup Details" ' Add label the user will see
combo.OnAction = "LookupDetailsFunction" ' Add the name of a function to call
' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.Caption = "Delete Record" ' Add a label the user will see
combo.OnAction = "DeleteRecordFunction" ' Add the name of the function to call
End With
End Sub
Since three function have been referenced, we can move on to define these as follows-
由于已引用三项功能,我们可继续按以下方式界定─
getText: Note, this option requires a reference to both the name of the Command Bar menu name as well as the name of the control caption.
getText:注意,此选项需要引用Command Bar菜单名称的名称以及控件标题的名称。
Public Function getText() As String
getText = CommandBars("MyListControlContextMenu").Controls("Lookup Text:").Text
' You could optionally do something with this text here,
' such as pass it into another function ...
MsgBox "You typed the following text into the menu: " & getText
End Function
LookupDetailsFunction: For this example, I will create a shell function and return the text "Hello World!".
LookupDetailsFunction:对于这个例子,我将创建一个shell函数并返回文本“Hello World!”。
Public Function LookupDetailsFunction() As String
LookupDetailsFunction = "Hello World!"
MsgBox LookupDetailsFunction, vbInformation, "Notice!"
End Function
DeleteRecordFunction: For this example, I will ensure the control is still valid by checking it against null, and if still valid, will execute a query to remove the record from a table.
DeleteRecordFunction:对于这个例子,我将通过对null检查它来确保控件仍然有效,如果仍然有效,将执行查询以从表中删除记录。
Public Function DeleteRecordFunction() As String
If Not IsNull(Forms!MyFormName.Controls("MyListControlName").Column(0)) Then
Currentdb.Execute _
"DELETE * FROM [MyTableName] " & _
"WHERE MyKey = " & Forms!MyFormName.Controls("MyListControlName").Column(0) & ";"
MsgBox "Record Deleted", vbInformation, "Notice!"
End If
End Function
Note: For LookupDetailsFunction
, DeleteRecordFunction
and getText
functions, these must be within a public scope to work correctly.
注意:对于LookupDetailsFunction,DeleteRecordFunction和getText函数,这些函数必须在公共范围内才能正常工作。
Finally, the last step is to test the menu. To do this, open the form, right click on the list control and select one of the options from the popup menu.
最后,最后一步是测试菜单。要执行此操作,请打开表单,右键单击列表控件,然后从弹出菜单中选择一个选项。
Optionally button.FaceID
can be utilized to indicate a known office icon to associate with each instance of the menu popup control.
可选地,button.FaceID可用于指示与菜单弹出控件的每个实例相关联的已知办公室图标。
I found Pillai Shyam's work on creating a FaceID Browser Add-In to be very helpful.
我发现Pillai Shyam在创建FaceID浏览器加载项方面的工作非常有用。
参考:Microsoft FaceID
Try This
Sub Add2Menu()
Set newItem = CommandBars("Form View Popup").Controls.Add(Type:=1)
With newItem
.BeginGroup = True
.Caption = "Make Report"
.FaceID = 0
.OnAction = "qtrReport"
End With
End Sub
As you can see it will add item in "Form View Popup" Command Bar and when this item is clicked it will load procedure qtrReport
如您所见,它将在“窗体视图弹出”命令栏中添加项目,当单击此项目时,它将加载过程qtrReport
And use this function to see all Commandbars in Access
并使用此函数查看Access中的所有命令栏
Sub ListAllCommandBars()
For i = 1 To Application.CommandBars.Count
Debug.Print Application.CommandBars(i).Name
Next
End Sub
In order to replace the default shortcut menu with a menu that includes the default actions plus your custom actions, you have to create a custom shortcut menu that includes the default actions. There is no way to extend the default shortcut menu.
要使用包含默认操作和自定义操作的菜单替换默认快捷菜单,您必须创建包含默认操作的自定义快捷菜单。无法扩展默认快捷菜单。
Shortcut menus in Access 2003 and before are a special kind of toolbar. You create them the same way (more or less) that you create a custom toolbar. The UI is kind of weird, though, as there's a special place where you create them.
Access 2003及之前的快捷菜单是一种特殊的工具栏。您可以使用创建自定义工具栏的相同方式(或多或少)创建它们。然而,UI有点奇怪,因为有一个特殊的地方你可以创建它们。
To get started, right click the toolbar in your front-end Access MDB. Choose CUSTOMIZE. In the list of Toolbars, check off SHORTCUT MENUS. This will give you a list of all the built-in shortcut menus, except that they don't actually end up looking like that in real use. For instance, if right click on a form, you get this shortcut menu:
要开始使用,请右键单击前端Access MDB中的工具栏。选择CUSTOMIZE。在工具栏列表中,选中SHORTCUT MENUS。这将为您提供所有内置快捷菜单的列表,不同之处在于它们实际上看起来并不像实际使用中那样。例如,如果右键单击表单,则会显示以下快捷菜单:
Form Design
Datasheet View
PivotTable View
PivotChart View
Filter By Form
Apply Filter/Sort
Remove Filter/Sort
Cut
Copy
Paste
Properties
Now, where is this menu on the shortcut menu? Well, this one happens to be the FORM VIEW TITLE BAR menu, even though it pops up any time you click anywhere other than on a control on the form. So, if that's the menu you want to alter, you could edit it by adding menu items to it (a drag-and-drop operation).
现在,快捷菜单上的这个菜单在哪里?好吧,这个恰好是FORM VIEW TITLE BAR菜单,即使它在任何时候点击表单上的控件以外的任何地方弹出。因此,如果这是您想要更改的菜单,您可以通过向其添加菜单项来进行编辑(拖放操作)。
I think it's actually better (as I said above) to create a custom shortcut menu that replicates the built-in menu and add your enhancements because this allows you to retain the Access default shortcut menu while also having your customized version of it for use when you want it. In that case, you'd need to start a new shortcut menu, and here's where the UI is weird:
我认为实际上更好(如上所述)创建一个自定义快捷菜单,复制内置菜单并添加增强功能,因为这样可以保留Access默认快捷菜单,同时还可以使用自定义版本的菜单。你想要它。在这种情况下,您需要启动一个新的快捷菜单,这里的UI很奇怪:
You click on the last choice on the shortcut menu, CUSTOM. You see it drops down a placeholder. You can't drag/drop to it. Instead, you have to click NEW in the main toolbar editing window and create a new shortcut toolbar (give it the name you want your custom shortcut menu to have). Your new toolbar now shows up in the list of toolbars. Highlight it and click PROPERTIES, and change the type to POPUP. This will give you an informative warning that this alteration changes it from a toolbar to a shortcut menu. You can then close your toolbar/shortcut menu's properties sheet, and now if you check off SHORTCUT MENUS again and look at the CUSTOM menu, you'll see your newly-created menu. Now you can drag and drop the menu items for the built-in menu to your new menu -- but don't drop them on the menu itself, but on the placeholder in the flyout from the > to the right of the menu name.
单击快捷菜单上的最后一个选项CUSTOM。你看它掉落了一个占位符。你无法拖放到它。相反,您必须在主工具栏编辑窗口中单击“新建”并创建一个新的快捷工具栏(为其指定您希望自定义快捷菜单具有的名称)。您的新工具栏现在显示在工具栏列表中。突出显示它并单击“属性”,然后将类型更改为“POPUP”。这将为您提供信息警告,表明此更改会将其从工具栏更改为快捷菜单。然后,您可以关闭工具栏/快捷菜单的属性表,现在如果再次选中SHORTCUT MENUS并查看CUSTOM菜单,您将看到新创建的菜单。现在,您可以将内置菜单的菜单项拖放到新菜单中 - 但不要将它们放在菜单本身上,而是放在菜单名称>右侧的弹出按钮中的占位符上。
You can then drag and drop any options you want from any menus or toolbars to your custom menu.
然后,您可以将任何菜单或工具栏中的任何选项拖放到自定义菜单中。
I assume you know how to use the shortcut menu, as it's part of the properties sheet of all form objects.
我假设您知道如何使用快捷菜单,因为它是所有表单对象的属性表的一部分。
UPDATE 2009/05/21: The official Access 2007 blog just posted an article on doing this programmatically in Access 2007. Because of the ribbon interface, there are going to be differences, but some things will be the same.
更新2009/05/21:官方Access 2007博客刚刚发布了一篇关于在Access 2007中以编程方式执行此操作的文章。由于功能区界面,会有差异,但有些事情会是相同的。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/04/20/8481dd4dfdd262e845ab9e95624c8c3f.html。