Solved

VBA Popup menu on a listbox entry in Access

Posted on 2011-03-24
7
1,648 Views
Last Modified: 2012-05-11
Hi
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)
    With cbListBoxActions
        .Enabled = True
    End With
    ' Add actions
    Set cbDeleteEntry = cbListBoxActions.Controls.Add(msoControlPopup, , , , True)
    With cbDeleteEntry
        .Caption = "Delete entry"
        .Enabled = True
        .OnAction = "DeleteListEntry"
        .Visible = True
    End With
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
        CommandBars("lstActions").ShowPopup
    End If

End Sub
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()
    Me.lstBlockName.RemoveItem Me.lstBlockName.ListIndex
End Sub
- 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
Bernard

0
Comment
Question by:bthouin
  • 3
  • 3
7 Comments
 
LVL 75
ID: 35208829
Just an FYI ... I've built 1000's of custom Menu, Toolbars and Right Click menus ...w/o a single line of code.

Everything you need is here (A2003 and prior only)- no code ... see this link:

http://www.jamiessoftware.tk/articles/menubars.html
       
Not sure it that article mentions it, but ... when you are in the Customize mode (dialog) ... if you hold down the Control key, you can drop and drag (ie, copy) menu items from one place to another ... handy for making a new menu that is similar another one.

mx
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 35209011
Take a look at the VBA (.bas) module attached in my solution to this question:

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26725466.html
0
 
LVL 1

Author Closing Comment

by:bthouin
ID: 35209487
Well, I inspired myself from the example in your answer, and it finally started to work properly ! So thanks a lot. I just have one more question: when I click on one meu entry and it does its work, then afterwards, the standard right-mouse click Access context menu appears ! How can I get rid of that ?

Regards
Bernard
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 35209648
How did you actually implement the popup?  Did you use the Commandbars().ShowPopup method?

If so, you probably have a shortcut menu already associated with the listbox, check that property and either delete it, or change it to the name of your custom commandbar.
0
 
LVL 1

Author Comment

by:bthouin
ID: 35211702
>>you probably have a shortcut menu already associated with the listbox, check that property<<
How ?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 35211886
In form design view, right click on the listbox and display the properties dialog box.  Select the Other tab and look at the Shortcut Menu and Shortcut Menu Bar properties.
0
 
LVL 1

Author Comment

by:bthouin
ID: 35226625
Ah, thanks a lot, I didn't notice there was a control attribute for shortcuts, as I never created any until recently :-). What I did though was to change the startup options and hide all menus (as we don't use menus at all), it works, but that's a bit  of a sledgehammer-to-kill-a-fly method, I guess...
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question