Solved

VBA Popup menu on a listbox entry in Access

Posted on 2011-03-24
7
1,591 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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
Comment Utility
>>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)
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 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

11 Experts available now in Live!

Get 1:1 Help Now