[Last Call] Learn how to a build a cloud-first strategyRegister Now


Access 2010: Create custom popup menu to run custom functions

Posted on 2012-08-15
Medium Priority
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
  • 2
LVL 14

Accepted Solution

pteranodon72 earned 2000 total points
ID: 38301900

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

ID: 38301926
LVL 52

Author Closing Comment

by:Gustav Brock
ID: 38304257
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!


Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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…
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…
Suggested Courses

830 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