0

I have a treeview on my form and I'm trying to create a right click context menu that gives some options for a particular node. This is my first time using the CommandBar functionality.

In the form with the Treeview, there is the following subroutine:

Public Sub MyTreeview_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Long, ByVal y As Long)
    
'Clicking right mouse button activates subroutine
If Button = 2 Then
    RightClickNodeOptions
End If

End Sub

Then in a separate module I've created a test subroutine to check the functionality:

Public Sub RightClickNodeOptions()

Dim cmdBAR As CommandBar
Set cmdBAR = CommandBars.Add(, msoBarPopup, False, True)

Dim cmdButtonAddChild As CommandBarButton
Set cmdButtonAddChild = cmdBAR.Controls.Add(msoControlButton)

Dim cmdButtonAddSibling As CommandBarButton
Set cmdButtonAddSibling = cmdBAR.Controls.Add(msoControlButton)

cmdButtonAddChild.Caption = "Add Child to Tree"
cmdButtonAddChild.OnAction = MsgBox("Child")

cmdButtonAddSibling.Caption = "Add Sibling to Tree"
cmdButtonAddSibling.OnAction = MsgBox("Sibling")

cmdBAR.ShowPopup

Set cmdBAR = Nothing
Set cmdButtonAddChild = Nothing
Set cmdButtonAddSibling = Nothing

End Sub

When I right click in the Treeview, both message boxes automatically pop up in order ("Child", "Sibling") before I've had a chance to select an option, then the CommandBar pops up with the two options. If I then click in one of the options in the commandbar, nothing happens.

Gabe R
  • 27
  • 5
  • Tried to replicate code. My TreeView control does not have a MouseUp event. – June7 Nov 08 '20 at 19:29
  • Does this answer your question? [How to add a menu item to the default right click context menu](https://stackoverflow.com/questions/770425/how-to-add-a-menu-item-to-the-default-right-click-context-menu). Or https://learn.microsoft.com/en-us/office/vba/access/concepts/miscellaneous/create-a-shortcut-menu-for-a-form-form-control-or-report – June7 Nov 08 '20 at 20:35
  • Create two functions that have the MsgBox() code. Call those functions from the custom menu. – June7 Nov 08 '20 at 21:02
  • Yes, I went ahead and used functions and that seemed to resolve the issue. Thanks! – Gabe R Nov 17 '20 at 15:30

1 Answers1

0

Personally, I don't make it temporary (so I can call it by name) but instead I delete and recreate it as needed.

Change the sub to a function to check the context menu was created successfully to avoid errors and then call its ShowPopup() method. You also need to cancel the event as it messes up with the context menu.

Private Function CreateRightClickNodeOptions() As Boolean
    
'delete existing
On Error Resume Next
CommandBars("NodeOptions").Delete
On Error GoTo Trap

    Dim cmdBAR As CommandBar
    Set cmdBAR = CommandBars.Add("NodeOptions", msoBarPopup, False, False)

    '... 
    ' remove ShowPopup() method from here

    'all good
    CreateRightClickNodeOptions= True
    
Leave:
    On Error GoTo 0
Exit Function

Trap:
    MsgBox Err.Description, vbCritical
    Resume Leave
End Function

Then, in your event handler check for right click and the context menu was created successfully before calling it.

Public Sub MyTreeview_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Long, ByVal y As Long)
    
    If Button = acRightButton Then
        If Not CreateRightClickNodeOptions() Then Exit Sub
        CommandBars("NodeOptions").ShowPopup
        DoCmd.CancelEvent
    End If

End Sub

Obviously if you don't have dynamic inputs you can create it only once and leave it, but if you need to pass varying parameters to the methods, you need to delete and recreate it.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kostas K.
  • 8,293
  • 2
  • 22
  • 28