0

I tried to get current record ID on right click commandBar menu (contextual menu) to open a linked report. I work on MS-ACCESS 2013

I tried to used the following code (Open Current Record from Right-Click CommandBar Menu) but I always get the first ID given by the multicolumn and not the active one.

Here is the form code

Private Sub Form_Load()
     CreateFormShortcutMenu_EmployePayList
End Sub


Private Sub CreateFormShortcutMenu_EmployePayList()

    Dim sMenuName As String
    sMenuName = "cmdShortCutMenu_EmployePayList"

    On Error Resume Next
    CommandBars(sMenuName).Delete
    If Err.Number <> 0 Then Err.Clear
    On Error GoTo 0

    Dim cmbRightClick As Office.CommandBar
    Dim cmbControl As Office.CommandBarControl

    Set cmbRightClick = CommandBars.Add(sMenuName, msoBarPopup, False, False)

    With cmbRightClick

        Set cmbControl = .Controls.Add(msoControlButton, 539, , , True)
        cmbControl.Caption = "New record"

        Set cmbControl = .Controls.Add(msoControlButton, 644, , , True)
        cmbControl.Caption = "Delete record"

        ' Add the Copy command
        Set cmbControl = .Controls.Add(msoControlButton, 19, , , True)
        cmbControl.Caption = "Copy"
        cmbControl.BeginGroup = True

        ' Add the Copy command
        Set cmbControl = .Controls.Add(msoControlButton, 22, , , True)
        cmbControl.Caption = "Paste"

        ' Add View TicketPay command
        Set cmbControl = .Controls.Add(msoControlButton, , , , True)
        With cmbControl
            .BeginGroup = True
            .Caption = "Ticket pay"
            .Parameter = Me.IdPay
            .OnAction = "=CallbackOpenTicketPay()"
            .FaceId = 65
        End With

    End With

    Me.ShortcutMenu = True
    Me.ShortcutMenuBar = sMenuName

    Set cmbControl = Nothing
    Set cmbRightClick = Nothing

End Sub 

Here is the code of the callback function (in a separate module)

Option Compare Database
Option Explicit

Public Function CallbackOpenTicketPay()
    Dim cbar As CommandBarControl

    Set cbar = CommandBars.ActionControl
    If cbar Is Nothing Then
        Debug.Print "CBar is nothing"
        Exit Function
    End If

    Dim IdPay
    IdPay = cbar.Parameter

    MsgBox IdPay
    MsgBox Screen.ActiveForm.ActiveControl.Form.IdPay

End Function

The first message box return the first record ID display in the subform. I get an alternative using the second call but I do not understand why the first one do not work.

  • I tested your code. I get "Invalid reference to form or report" error on the second MsgBox. Had to fix reference because I am not using subform, simply `MsgBox Screen.ActiveForm.ActiveControl`. Now works without error as long as control is not Null. First MsgBox always returns first ID. Possibly because that is value loaded when menu is built? – June7 Apr 02 '19 at 23:29
  • I can't find anything that shows successful use of Parameter property nor passing value via function argument. Sorry, I give up. – June7 Apr 03 '19 at 06:14

0 Answers0