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

时间:2019-04-29 09:51:53

标签: vba ms-access-2016

问题中的解决方案启发了我问题中的代码:
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"。它会导致显示一个conextual菜单,但是当我单击其中一个按钮时,什么也没有发生。

1 个答案:

答案 0 :(得分:2)

这里有一些问题。

        combo.OnAction = DelAction()

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

        combo.OnAction = "DelAction()"

由于DelAction()在您的表单模块中,因此仍然无法使用。
将函数移到带有参数的公共模块中,或在那里硬编码对象名称,

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

或尝试(不确定是否可行):

        combo.OnAction = "Form_YourFormName_DelAction()"

"List_actions_DblClick"相同-函数需要“从外部”调用,就像在“即时”窗口中一样。


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