Monday, January 25, 2010

The Last Statement

Removing menu items using the API wrapper functions


Using the API wrapper functions, you can literally remove items from a menu bar. You can disable a menu by using SetMenuItem macro or VBA method, but there are occasions when you just do not want the menu item to appear. The API is the only way to remove a menu item.


After you remove a menu item, you still can execute that menu item by using the DoMenuItem macro or VBA method. You cannot however, use the SendKeys to access a menu item. You should avoid using SendKeys to acess menu items anyway, because your code will be very difficult to follow. Also avoid using the function or macro that perform the menu item.


To remove a menu item, call the wrapper function RemoveMenuItem.

You need to pass only two parameters to the function, the 0-based index of the pull-down menu and the 0-based index of the menu item to remove. These are identical to the parameters used for the SetMenuItem method.

If you want to insert the removed menu item later, you need to know not only the indexes of the menu item, but also the menu item text and submenu handle or ActionID. If the menu you remove opens a submenu, you will need the handle or pointer of the submenu. If the menu item does not open a menu, you must know the ActionID that Access needs to know what to perform when the menu item is selected.


When you see the wrapper function RemoveMenuItem to remove a menu item, the menu text and ActionID/submenu handle are stored in two global variables:


§ The menu text is stored in the variable szMenuText.

§ The ActionID or submenu handle are stored in LMenuID.


You want to copy these to your own variables because the global variable values are overwritten with new values each time you remove a menu item.


Inserting menu item using API wrapper functions


You can insert a menu item by using the Windows API, just as you can remove menu item. You can insert any new menu item, however, because a menu item needs a valid ActionID, you should insert only removed menu items. If you insert a menu item without a valid ActionID, the menu item appears but will not perform any function when selected.


The first two parameters are just like RemoveMenuItem. They are respectively, the index of the pull-down menu and the menu item itself. In addition to the positional indexes, you must also supply the text to display for the menu item, as well as the ActionID or submenu handle. If you move these values to your own variables when you removed a menu item, you simply need to pass them to the InsertMenuItem function.

If you will be removing many menu items, you should create an array to hold the text and ActionID/submenu information of the removed item.


You can reinsert a menu in a different location on the same pull-down after removing the menu. However, if you insert the menu on a different pull-down menu, when the user selects the menu item your application will encounter a running error and stop. Also, if you change the text of the menu item when you insert it a runtime error will be triggered when the menu item is selected. If you do not insert the menu with a valid ActionID, the menu item appears but will not perform any function when selected.


If you change the text of a menu item, Access return an error when you select the item Access uses the text of the menu item as a reference to the name of the macro that the item executes. If you need to change a menu item’s text, you should create two menu items in your menu macro ─ one for each state of the text. Remove one of the menu items when the form is loaded, when the text needs to be changed, simply remove and insert the desired menu items.

No comments: