VBA Popup menu on a listbox entry in Access
Posted on 2011-03-24
I'm trying for the first time to add a popup menu to an Access application, and this entirely with VBA. What seemed a simple job is not working well, I must say. After quite a bit of trial and error, I've got close, but it still doesn't work. Let me explain:
I have one main form, and in that form, a listbox, on which I'd like to show, on a right-click of the mouse on an entry, a popup menu with 3 menu entries. I have added a module with code to that effect, here the code showing the basics and one of the 3 popup entries:
Public cbListBoxActions As CommandBar
Public Sub addCommandBar()
Dim cbDeleteEntry As CommandBarPopup
Dim cbMoveEntryUp As CommandBarPopup
Dim cbMoveEntryDown As CommandBarPopup
' Popup menu stuff
Set cbListBoxActions = CommandBars.Add("lstActions", msoBarPopup, , True)
.Enabled = True
' Add actions
Set cbDeleteEntry = cbListBoxActions.Controls.Add(msoControlPopup, , , , True)
.Caption = "Delete entry"
.Enabled = True
.OnAction = "DeleteListEntry"
.Visible = True
I call this subroutine at the end of the main form Load event. When I now right-mouse-click on an entry in the listbox, the following code of the form is fired:
Private Sub lstBlockName_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Invoke the menu on a right click on the list box
If Button = acRightButton Then
I then see my popup menu with my defined entries, BUT:
- they have a little arrow at the end of the text (i.e. pointing to a submenu, which I don't have and don't need)
- clicking on the menu entry does nothing, although I have defined a sub corresponding to the .OnAction of the popup entry, like this:
Public Sub DeleteListEntry()
- clicking on the little arrow at the end of the entry shows an empty "sub-entry", I guess (a greyish square without anything in it), and clicking on that doesn't do anything either
So my questions are:
- why do I have popup menu entries that think that there is yet a further level of entries ?
- why does nothing happen when I click on either menu entry or "sub-entry" ?
i.e. what should I change in my code to make the whole thing work ?
Thanks for help