Solved

VBA Popup menu on a listbox entry in Access

Posted on 2011-03-24
7
1,619 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now