How to Dim (Gray) Menu Items with Access Basic Summary: Access Basic does not have any intrinsic command that allows you to dim (make unavailable) a menu item. There are also no properties associated with a form that will allow you to set this menu characteristic. To change such characteristics of a menu item, you can use Windows API functions. For more information about the structure of the Access menu system, query on the following words in the Microsoft Knowledge Base: MENU and SYSTEM and FINDWINDOW and GETMENU and GETSUBMENU More Information: The following API function is used to dim a menu item: EnableMenuItem% (hMenu%, wIDEnableItem%, wEnable%) -------------------------------------------------- This function enables, disables, or grays a menu item. hMenu% Specifies the menu. wIDEnableItem% Specifies the menu item to be checked. The wIDEnableItem% parameter can specify pop-up menu items as well as menu items. wEnable% Specifies the action to take. It can be a combination of MF_DISABLED, MF_ENABLED, and MF_GRAYED. These values can be combined by using the bitwise 'OR' operator. Return Value The return value specifies the previous state of the menu item. The return value is -1 if the menu item does not exist. Example ------- The example below designs a menu that will dim the menu item: 1. Create a new macro. After adding the following actions and their associated properties, save the macro and name it "Menu Manipulation Macro." To display the "Macro Name" column, choose the Macro Name command from the View menu. Macro Name Action Function Name ---------------------------------------------------------- GrayItem RunCode Gray_Menu_Item(0,0) UnGray RunCode UnGray_Menu_Item(0,0) 2. Create a new macro. After adding the action below and its associated property, save the macro as "Custom Demo Menu." Macro Name Action Function Name ---------------------------------------------------------- Top Level Menu AddMenu [Top Level Menu].AddMenu Action Arguments ----------------------------------------- Menu Name &Gray Menu Macro Name Menu Manipulation Macro 3. Create a new blank form and display the Properties window of the form by choosing the Properties command from the View menu. 4. Set the OnMenu property to "Custom Demo Menu." 5. From the File menu, choose Close to close the form. Save the form as "Menu Manipulation Form." 6. Create a new module from the Database window. Within the new module, enter the Access Basic code listed further below. Save the module as "Menu Manipulation Code." 7. From the Database window, select the Menu Manipulation Form and then choose the Open button to display the form in Form view. The normal Access menu will disappear and be replaced by the custom menu you created with the above steps. 8. There are two options in the menu. Choose the GrayItem command and the command will be dimmed (grayed). Choosing the UnGray command will make the GrayItem menu command available. '******************************************************************** 'Declarations section of the module. '******************************************************************** Option Explicit 'Note: Each Declaration must be placed on a single line. Declare Function FindWindow% Lib "user" (ByVal lpClassName As Any, ByVal lpCaption As Any) Declare Function GetMenu% Lib "user" (ByVal hWnd%) Declare Function GetSubMenu% Lib "user" (ByVal hSubMenu%, ByVal nPos%) Declare Function EnableMenuItem% Lib "user" (ByVal hMenu%, ByVal wItem%, ByVal wEnable%) Const MF_BYPOSITION = &H400 Const MF_GRAYED = &H1 Const MyNull = 0& Const ClassName = "OMain" Dim ChWnd% 'handle to the Microsoft Access window. Dim hMenuTop% 'handle to the Microsoft Access menu. Dim hSubMenu% 'handle to the pop-up menu Dim ItemID% 'command ID associated with menu item. Dim ReturnVal% '=========================================================== 'This function will initialize: ' ' - The window handles associated with the Access form. ' - The handle to the menu of the specified window. ' - Menu handle of the specified pop-up menu of the window menu. ' 'The variables here are global to the database. '=========================================================== Sub Get_Menu_Handles (TopLevel%) ChWnd% = FindWindow(ClassName, MyNull) hMenuTop% = GetMenu(ChWnd%) hSubMenu% = GetSubMenu(hMenuTop%, TopLevel%) End Sub '=========================================================== 'This function will dim a menu item. The text of a dimmed 'menu item is displayed in light gray text on the menu, 'but does not allow the user to select the item either by 'mouse or keypad. The macro action associated with the 'menu item will not execute when the user tries to select 'the menu item. '=========================================================== Function Gray_Menu_Item (TopLevel%, SubLevel%) Call Get_Menu_Handles(TopLevel%) Gray_Menu_Item = EnableMenuItem(hSubMenu, SubLevel%, MF_GRAYED Or MF_BYPOSITION) End Function '=========================================================== 'This function will not ungray a menu item that also enables 'the menu item so the user can select the item and run the 'macro associated with the menu. '=========================================================== Function UnGray_Menu_Item% (TopLevel%, SubLevel%) Call Get_Menu_Handles(TopLevel%) UnGray_Menu_Item = EnableMenuItem(hSubMenu, SubLevel%, Not MF_GRAYED And MF_BYPOSITION) End Function