Solved

VBA Popup menu on a listbox entry in Access

Posted on 2011-03-24
7
1,695 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

740 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