The goal is to create menus which can be utilized with certain controls on an MS Access form and to be able to right click on a that control, for example on a listbox and a relevant context specific menu popup with options, which if clicked, would trigger a predefined subroutine or function.
What is the best method to accomplish this programmatically?
I am using MS Access 2003 and would like to do this using VBA.
In order to replace the default shortcut menu with a menu that includes the default actions plus your custom actions, you have to create a custom shortcut menu that includes the default actions. There is no way to extend the default shortcut menu.
Shortcut menus in Access 2003 and before are a special kind of toolbar. You create them the same way (more or less) that you create a custom toolbar. The UI is kind of weird, though, as there's a special place where you create them.
To get started, right click the toolbar in your front-end Access MDB. Choose CUSTOMIZE. In the list of Toolbars, check off SHORTCUT MENUS. This will give you a list of all the built-in shortcut menus, except that they don't actually end up looking like that in real use. For instance, if right click on a form, you get this shortcut menu:
Now, where is this menu on the shortcut menu? Well, this one happens to be the FORM VIEW TITLE BAR menu, even though it pops up any time you click anywhere other than on a control on the form. So, if that's the menu you want to alter, you could edit it by adding menu items to it (a drag-and-drop operation).
I think it's actually better (as I said above) to create a custom shortcut menu that replicates the built-in menu and add your enhancements because this allows you to retain the Access default shortcut menu while also having your customized version of it for use when you want it. In that case, you'd need to start a new shortcut menu, and here's where the UI is weird:
You click on the last choice on the shortcut menu, CUSTOM. You see it drops down a placeholder. You can't drag/drop to it. Instead, you have to click NEW in the main toolbar editing window and create a new shortcut toolbar (give it the name you want your custom shortcut menu to have). Your new toolbar now shows up in the list of toolbars. Highlight it and click PROPERTIES, and change the type to POPUP. This will give you an informative warning that this alteration changes it from a toolbar to a shortcut menu. You can then close your toolbar/shortcut menu's properties sheet, and now if you check off SHORTCUT MENUS again and look at the CUSTOM menu, you'll see your newly-created menu. Now you can drag and drop the menu items for the built-in menu to your new menu -- but don't drop them on the menu itself, but on the placeholder in the flyout from the > to the right of the menu name.
You can then drag and drop any options you want from any menus or toolbars to your custom menu.
I assume you know how to use the shortcut menu, as it's part of the properties sheet of all form objects.
UPDATE 2009/05/21: The official Access 2007 blog just posted an article on doing this programmatically in Access 2007. Because of the ribbon interface, there are going to be differences, but some things will be the same.
Try This
As you can see it will add item in "Form View Popup" Command Bar and when this item is clicked it will load procedure qtrReport
And use this function to see all Commandbars in Access
First create an
_MouseUp
event to execute on the respective control looking to see if the right mouse button was clicked and if so, call the.ShowPopup
method.Of course this assumes the
Since at this point the Command Bar
MyListControlContextMenu
is undefined, I define the Menu in a separate module as follows:Since three function have been referenced, we can move on to define these as follows-
getText: Note, this option requires a reference to both the name of the Command Bar menu name as well as the name of the control caption.
LookupDetailsFunction: For this example, I will create a shell function and return the text "Hello World!".
DeleteRecordFunction: For this example, I will ensure the control is still valid by checking it against null, and if still valid, will execute a query to remove the record from a table.
Note: For
LookupDetailsFunction
,DeleteRecordFunction
andgetText
functions, these must be within a public scope to work correctly.Finally, the last step is to test the menu. To do this, open the form, right click on the list control and select one of the options from the popup menu.
Optionally
button.FaceID
can be utilized to indicate a known office icon to associate with each instance of the menu popup control.I found Pillai Shyam's work on creating a FaceID Browser Add-In to be very helpful.
References: Microsoft FaceID