gpt4 book ai didi

ms-access - 如何将菜单项添加到默认的右键单击上下文菜单

转载 作者:行者123 更新时间:2023-12-03 07:55:47 25 4
gpt4 key购买 nike

目标是创建可与 MS Access 表单上的某些控件一起使用的菜单,并能够右键单击该控件,例如在列表框和带有选项的相关上下文特定菜单弹出窗口上,如果单击,将触发预定义的子程序或函数。

以编程方式完成此任务的最佳方法是什么?

我正在使用 MS Access 2003 并希望使用 VBA 来执行此操作。

最佳答案

首先创建一个_MouseUp要在相应控件上执行的事件,查看是否单击了鼠标右键,如果是,则调用 .ShowPopup方法。

当然,这是假设

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

由于此时命令栏 MyListControlContextMenu未定义,我在单独的模块中定义菜单如下:
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

由于已经引用了三个函数,我们可以继续定义它们如下 -

getText:注意,此选项需要同时引用 的名称命令栏菜单名称 以及 的名称控制标题 .
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:对于本例,我将创建一个 shell 函数并返回文本“Hello World!”。
Public Function LookupDetailsFunction() As String

LookupDetailsFunction = "Hello World!"

MsgBox LookupDetailsFunction, vbInformation, "Notice!"

End Function

DeleteRecordFunction:对于这个例子,我将通过检查控件是否为空来确保控件仍然有效,如果仍然有效,将执行查询以从表中删除记录。
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

注意:对于 LookupDetailsFunction , DeleteRecordFunctiongetText功能,这些必须在公共(public)范围内才能正常工作。

最后,最后一步是测试菜单。为此,请打开表单,右键单击列表控件并从弹出菜单中选择其中一个选项。

可选 button.FaceID可用于指示与菜单弹出控件的每个实例相关联的已知办公室图标。

我找到了 Pillai Shyam's work对创建 FaceID 浏览器插件非常有帮助。

引用:
Microsoft
FaceID

关于ms-access - 如何将菜单项添加到默认的右键单击上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/770425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com