CommandBarControl 在单击按钮之前执行 .OnAction

CommandBarControl executing .OnAction before click on button

我问题中的代码受此问题提供的答案中的解决方案启发:
How to add a menu item to the default right click context menu

我在显示操作列表的表单上有一个 ListBox 对象。我希望用户能够右键单击此列表的项目以显示上下文菜单,他可以在其中:

  1. 打开一个新窗体,他可以在其中查看和编辑操作(对应于在列表项上执行双击事件)
  2. 从列表中删除项目

    Private Sub List_actions_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single
    
        'set up commandBar
        Dim combo As CommandBarControl
    
        '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("RCActionContextMenu").Delete
        On Error GoTo 0
    
        'Make this menu a popup menu
        With CommandBars.Add(Name:="RCActionContextMenu", Position:=msoBarPopup)
            Set combo = .Controls.Add(Type:=msoControlButton)
                combo.BeginGroup = True
                combo.Caption = "View action"              ' Add label the user will see
                combo.OnAction = "List_actions_DblClick"    'Add the name of a function to call
    
            Set combo = .Controls.Add(Type:=msoControlButton)
                combo.Caption = "Delete action"
                combo.OnAction = DelAction()
        End With
    
        If Button = acRightButton Then
            DoCmd.CancelEvent
            CommandBars("RCActionContextMenu").ShowPopup
        End If
    End Sub
    
    Public Function DelAction()
        If Not IsNull(Me.Controls("RCActionContextMenu").Column(0)) Then
            CurrentDb.Execute "DELETE * FROM T_ACTIONS " & _
                      "WHERE ID = " & List_actions.Column(9) & ";"
            MsgBox "Action supprimée", vbInformation, "Information"
        End If
    End Function
    
    Private Sub List_actions_DblClick(Cancel As Integer)
        Dim vStatus As String
    
        'Get the record's index of the action
        rowNumber = Me.List_actions.ListIndex + 1
    
        id_action = List_actions.Column(9, rowNumber)
        vStatus = List_actions.Column(5, rowNumber)
    
        'Open the action
        DoCmd.OpenForm "F_ACTIONS", , , "[ID] = " & List_actions.Column(9)
    
        Form_F_ACTIONS.Effective_date.Visible = Effdatefunction(vStatus)
    
    End Sub
    

我遇到的问题是 DelAction() 函数在弹出窗口显示之前执行,我得到 run-time error 2465 说明 "Microsoft Access can't find the field 'RCActionContextMenu' referred to in your expression."
我已经尝试将行 combo.OnAction = DelAction() 替换为 combo.OnAction = "DelAction"。它会导致上下文菜单自行显示,但当我单击任一按钮时没有任何反应。

这里有一些问题。

        combo.OnAction = DelAction()

如您所见,这将调用该函数。这里需要设置一个字符串。

        combo.OnAction = "DelAction()"

这仍然行不通,因为 DelAction() 在您的表单模块中。
将函数移动到带有参数的 public 模块,或者在那里硬编码对象名称,

combo.OnAction = "DelAction(""MyFormName"", ""List_actions"")"

或尝试(不确定是否有效):

        combo.OnAction = "Form_YourFormName_DelAction()"

"List_actions_DblClick" 相同 - 函数需要被调用 "from the outside",就像从 Immediate window 中一样。


If Not IsNull(Me.Controls("RCActionContextMenu").Column(0)) Then

你的上下文菜单命令栏不是一个控件,你想要的是列表框:

If Not IsNull(Me.Controls("List_actions").Column(0)) Then

或者干脆

If Not IsNull(Me!List_actions.Column(0)) Then

删除动作后,需要重新查询列表框。

CurrentDb.Execute "DELETE * FROM T_ACTIONS " ...
Me!List_actions.Requery