1

I try to call an Function via an CommandBarButton-Event. The Code looks finde so far, I also get no Error, but nothing happens. What am I doing wrong? My Code looks like this:

Private Sub Main()
    ContextMenu1
    CommandBars("MyListControlContextMenu").ShowPopup
End Sub

Private Sub ContextMenu1()
    Dim MenuItem As CommandBarPopup
    On Error Resume 
    NextCommandBars("MyListControlContextMenu").Delete
    On Error GoTo 0

    With CommandBars.Add(Name:="MyListControlContextMenu", Position:=msoBarPopup, MenuBar:=False, Temporary:=True)
        Set MenuItem = .Controls.Add(Type:=msoControlPopup)
        With MenuItem
            .Caption = "main"
            Dim MenuButton As CommandBarButton
            Set MenuButton = MenuItem.Controls.Add(Type:=msoControlButton)
            With MenuButton
                .Caption = "sub"
                .OnAction = "Test1"
            End With
        End With
    End With
End Sub

Private Sub Test1()
    MsgBox "test", vbOKOnly, "test"
End Sub
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

2

It seems like the correct syntax for MS-Access 2010 VBA when implementing an event inside a contextmenu looks like this:

Sub MyContextMenuConstructor()
    ...
    With MenuButton
        .Caption = "myCaption"
        .OnAction = "=MyFunction('" & myParameter & "')"
    End With
    ...
End Sub

Private Function MyEvent(myInputParameter As String)
    MsgBox myInputVariable, vbOKOnly
End Function

Remember:

- What ever called by the Event, needs to be a Function, no Sub; 
- It needs to be called with brackets, even if you do not pas a parameter; 
- When you call the Function, you must have an '=' before its name and you have to have an apostrophe before and after your Parameter. Also allways place a Whitespace between '&' and '"', since VBA doesn't seem to be that clever.