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

ID: Q88940


The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0


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 "SetMenuBar action," and then "Make a custom menu commands appear dimmed or selected" 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:
  1. 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.


  2. 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 


  3. Create a new blank form.


  4. Set the MenuBar property to the Custom Demo Menu macro.

    NOTE: In version 1.x the MenuBar property is called the OnMenu property.


  5. Save the form as Menu Manipulation Form.


  6. 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.

    
       '********************************************************************
       '            Declarations section of the module
       '********************************************************************
          Option Explicit
    
    
          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%)
          Declare Function IsZoomed% Lib "User" (ByVal hWnd%)
          Declare Sub DrawMenuBar Lib "User" (ByVal hWnd%)
    
          Const MF_BYPOSITION = &H400
          Const MF_GRAYED = &H1
          Const MF_UNGRAYED = &H0
    
          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 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 


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


Additional query words: disabled enabled

Keywords : kbprg
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: March 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.