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.
Best Answer
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