Microsoft KB Archive/88940

= ACC: How to Dim (Gray) Menu Items with Access Basic =

Article ID: 88940

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q88940



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

Access Basic does not have a command that enables you to make unavailable (to dim or gray) a menu item. There are also no form properties that enable you to set this menu characteristic. However, you can use Windows application programming interface (API) functions to change such menu item characteristics.

NOTE: In Microsoft Access for Windows 95 version 7.0, you can accomplish this task by using a macro or by using Visual Basic for Applications. For more information about making a menu item unavailable, search for &quot;SetMenuBar action,&quot; and then &quot;Make a custom menu commands appear dimmed or selected&quot; using the Microsoft Access for Windows 95 Help Index.

NOTE: These API functions will only work for custom menus created with macros. They will not work for the default Microsoft Access menus. This technique will also not work for custom menu items created with the DoMenuItem action. For example, if you add a command to the menu with the DoMenuItem action and specify the PrintPreview arguments for the action, Microsoft Access overrides any API function that you write to check or to dim this menu command.



MORE INFORMATION
The following Windows API function is used to make a menu item appear dimmed:

   EnableMenuItem% (hMenu%, wIDEnableItem%, wEnable%)

This function enables or disables a menu item as follows:

  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 following example designs a menu that makes a menu item appear dimmed:

  Create the following new macro and save the macro as Menu Manipulation Macro:

     Macro Name             Action        Function Name --     GrayItem               RunCode       Gray_Menu_Item(0,0) UnGray                RunCode       UnGray_Menu_Item(0,0) When determining the position of a menu command, separator bars count as commands. For example, to reference the position of the Import command on the File menu in the Database window, use the arguments 0 (for the File menu) and 7 (for the Import command). Positions 3 and 6 correspond to the separator bars on the File menu.   Create the following new macro and 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  Create a new blank form. Set the MenuBar property to the Custom Demo Menu macro.

NOTE: In version 1.x the MenuBar property is called the OnMenu property. Save the form as Menu Manipulation Form.</li>  Create a new module, type the following code, and then save the module as Menu Manipulation Code.

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code.

<pre class="fixed_text">  '******************************************************************** '           Declarations section of the module '********************************************************************     Option Explicit

Declare Function FindWindow% Lib &quot;user&quot; (ByVal lpClassName_                         As Any, ByVal lpCaption As Any) Declare Function GetMenu% Lib &quot;user&quot; (ByVal hWnd%) Declare Function GetSubMenu% Lib &quot;user&quot; (ByVal hSubMenu%,_                         ByVal nPos%) Declare Function EnableMenuItem% Lib &quot;user&quot; (ByVal hMenu%,_                         ByVal wItem%, ByVal wEnable%) Declare Function IsZoomed% Lib &quot;User&quot; (ByVal hWnd%) Declare Sub DrawMenuBar Lib &quot;User&quot; (ByVal hWnd%)

Const MF_BYPOSITION = &H400 Const MF_GRAYED = &H1 Const MF_UNGRAYED = &H0

Const MyNull = 0& Const ClassName = &quot;OMain&quot;

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 initializes: '     ' - The window handles associated with the Microsoft Access form. ' - The handle to the menu of the specified window. ' - The 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 dims a menu item. The text of a dimmed 'menu item appears 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 does not execute when the user tries to select 'the menu item. '===========================================================     Function Gray_Menu_Item (TopLevel%, SubLevel%)

'If the form is maximized, the system menu is added to the forms 'menu bar, so increment the actual TopLevel% If (IsZoomed(Screen.ActiveForm.hWnd)) Then TopLevel% = TopLevel% + 1 End If

Call Get_Menu_Handles(TopLevel%) Gray_Menu_Item = EnableMenuItem(hSubMenu, SubLevel%,_                             MF_GRAYED Or MF_BYPOSITION)

DrawMenuBar ChWnd% End Function

'===========================================================     'This function does 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%)

'If the form is maximized, the system menu is added to the forms 'menu bar, so increment the actual TopLevel% If (IsZoomed(Screen.ActiveForm.hWnd)) Then TopLevel% = TopLevel% + 1 End If

Call Get_Menu_Handles(TopLevel%) UnGray_Menu_Item = EnableMenuItem(hSubMenu, SubLevel%,_                               MF_UNGRAYED Or MF_BYPOSITION)

DrawMenuBar ChWnd% End Function </li> Open the Menu Manipulation Form form in Form view. Note that the usual Microsoft Access menu disappears and is replaced by the custom menu you created in the previous steps.

There are two options on the new menu. Choose the GrayItem command and the command appears dimmed. Choose the UnGray command and the GrayItem menu command is available.</li></ol>

Additional query words: disabled enabled

Keywords: kbhowto kbprogramming KB88940

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.