Solved

VBA Popup menu on a listbox entry in Access

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

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 48

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 48

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

729 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