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

目标是创建可以与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未定义,我在单独的模块中定义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 

由于已引用三项功能,我们可继续按以下方式界定─

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:对于这个例子,我将创建一个shell函数并返回文本“Hello World!”.

Public Function LookupDetailsFunction() As String

   LookupDetailsFunction = "Hello World!"

   MsgBox LookupDetailsFunction, vbInformation, "Notice!"

End Function

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

注意:对于LookupDetailsFunction,DeleteRecordFunction和getText函数,这些函数必须在公共范围内才能正常工作.

最后,最后一步是测试菜单.要执行此操作,请打开表单,右键单击列表控件,然后从弹出菜单中选择一个选项.

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

我在创建FaceID浏览器加载项时发现Pillai Shyam’s work非常有帮助.

参考文献:
Microsoft
FaceID

转载注明原文:ms-access – 如何将菜单项添加到默认的右键单击上下文菜单 - 代码日志