Access 2010: Create custom popup menu to run custom functions

Posted on 2012-08-15
Last Modified: 2012-09-19
I need some help with this.

I've found out that you need a reference to the Office Object Library and then create a "commandbar" to achieve this, but the code examples I can locate just adds standard Access/Office commands to the menu using "secret" values.

My popup should be displayed when right-clicking the form pane outside the controls.
It should contain a few menu entries, say:

 Create New

When clicking one of these, a function of mine should be called, like:


I've managed to create a shortcutmenu (macro) but, when I right-click, no menu is shown - the macro is executed right away.

I would prefer the menu to be created as "temporary".

Question by:Gustav Brock
    LVL 14

    Accepted Solution


    I haven't worked with temporary context menus / shortcut menus / right-click-menus, but here is what I've learned from code on this site:

    with a reference to Office 'without reference
    Public Function CreateCMenu()
    On Error Resume Next
        Dim cmb As CommandBar 'Object
        Dim cmbBtn1 As CommandBarButton 'Object
        Dim cmbBtn2 As CommandBarButton 'Object
        Set cmb = CommandBars.Add("MyContext", _
                   msoBarPopup, False, False)    ' msoBarPopup = 5
            With cmb
                  ' add cut, copy, and paste buttons with the "magic number" technique that assigns
                  ' appearance and behavior. The magic number goes in as the second parameter
                .Controls.Add msoControlButton, _
                      21, , , True  ' 21=Cut, msoControlButton=1
                .Controls.Add msoControlButton, _
                          19, , , True  '19= Copy
                .Controls.Add msoControlButton, _
                          22, , , True  ' 22=Paste
    ' add customized buttons with our caption and function name -- second param is blank
                Set cmbBtn1 = .Controls.Add(msoControlButton, _
                                        , , , True)
                With cmbBtn1
                    .BeginGroup = True
                    .Caption = "Create New"
                    .OnAction = "=CreateNewOrder()"
                    .FaceID = 59  'smiley face
                End With
                Set cmbBtn2 = .Controls.Add(msoControlButton, _
                                        , , , True)
                With cmbBtn2
                    .Caption = "Reset"
                    .OnAction = "=ClearOrder()"
                End With
            End With
    End Function

    Open in new window

    By running this code once, you create the commandbar "MyContext" in the database.
    You can then go into your target form in design view,
    Properties->Other->Shortcut Menu=Yes
    Properties->Other->Shortcut Menu Bar=MyContext

    Add an AutoExec macro to run CreateCMenu (which deletes any existing MyContext and builds it from scratch) and then the proper context menu will be built with the correct name where ever you move your database.
    LVL 14

    Expert Comment

    LVL 48

    Author Closing Comment

    by:Gustav Brock
    Thank you so much! This is exactly what is needed.

    And just change the last parameter to True to make it temporary:

    Set cmb = CommandBars.Add("MyContext", _
                   msoBarPopup, False, True)  

    And thanks for the additional links as well.

    Have a nice weekend!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    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…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
    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…

    779 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

    10 Experts available now in Live!

    Get 1:1 Help Now