ACC: How to Check a Menu Item Using Access Basic

This article was previously published under Q90811
This article has been archived. It is offered "as is" and will no longer be updated.
Summary
Advanced: Requires expert coding, interoperability, and multiuser skills.

Microsoft Access does not have any built-in macro or Access Basic commandsyou can use to place a check mark beside or remove a check mark from amenu command. To do this in Microsoft Access, you must use Access Basiccode to call Microsoft Windows application programming interface (API)functions.

NOTE: In Microsoft Access 7.0 and 97, you can use the SetMenuItem macroaction to set the state of menu items (enabled or disabled, checked orcleared) on a custom menu bar or the global menu bar for the active window.For more information about the SetMenuItem action, search the Help Indexfor "SetMenuItem Action."
More information
The following Windows API function is used to check or clear a menuitem.

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

   CheckMenuItem (ByVal hMenu As Integer, _                  ByVal wIDCheckItem As Integer, _                  ByVal wCheck As Integer) _                  As Integer				


This function places check marks next to or removes check marks frommenu items in the pop-up menu specified by the hMenu% parameter asfollows:

   hMenu%         Identifies the handle to the menu.   wIDCheckItem%  Specifies the menu item to be checked.   wCheck%        Specifies how to check the menu item. The                  wCheck% parameter can be a combination of the                  MF_CHECKED or MF_UNCHECKED with MF_BYPOSITION or                  MF_BYCOMMAND flags. These flags can be combined                  by using the bitwise OR operator. The values are                  described as follows:                  Value          Meaning                  ----------------------------------------------------                  MF_BYCOMMAND   Specifies that the idCheckItem%                                 parameter gives the menu-item                                 identifier (MF_BYCOMMAND is the                                 default).                  MF_BYPOSITION  Specifies that the idCheckItem%                                 parameter gives the position of the                                 menu item (the first item is at                                 position zero).                  MF_CHECKED     Selects the item (adds check mark).                  MF_UNCHECKED   Clears the item (removes check mark).   Return Value   The return value specifies the previous state of                  the item. It is either MF_CHECKED or                  MF_UNCHECKED. The return value is -1 if the menu                  item does not exist.				


NOTE: Top-level menu items (items on the Microsoft Access menu bar) cannothave a check mark.

When determining the position of a menu command, separator bars count ascommands. For example, to get to the Import menu command on the File menuin the Database window, use the arguments 0 (for the File menu) and 7 (forthe Import command). Positions 3 and 6 correspond to the separator bars onthe File menu.

Example

The example below creates a menu, associated with a form, that can beused to check or clear a menu command:
  1. Create the following new macro and save it as Menu Checking:
          Macro Name             Action      ------------------------------      &Check Test            RunCode      &Check Test Actions      ----------------------------------      RunCode         Function Name: ChkMenuItem(0,0)						

    The first parameter in the function indicates which menu on the menu bar to use. All menus are zero based. For example, if your menu bar contains the File, Edit, Window, and Help menus, and you specify 2 as the first parameter for one of these functions, the function will work on the Window menu. A 2 indicates the Window menu instead of the Edit menu because the parameter starts at 0 instead of 1; therefore, 0 indicates the File menu, 1 indicates the Edit menu, and so on.

    The second parameter in the function indicates which menu command (on the menu indicated in the first parameter) to place the check mark beside. This parameter also starts at 0. To refer to the first command on the menu, use 0, to refer to the second, use 1, and so on.
  2. Create the following new macro and save the macro as Custom Menu.
          Macro Name    Action      ---------------------      Custom Menu   AddMenu      Custom Menu Actions      ---------------------------------------      AddMenu         Menu Name: &Menu Check         Menu Macro Name: Menu Checking						

  3. Create a new blank form.
  4. In Microsoft Access 1.x, set the form's OnMenu property to the Custom Menu macro. In Microsoft Access 2.0, set the form's MenuBar property to the Custom Menu macro.
  5. Save the form as Menu Checking.
  6. Create a module and type the following lines in the Declarations section:

    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 in Access Basic.

          Option Explicit      ' 16-bit API declarations.      Declare Function GetMenu Lib "user" (ByVal hWnd As Integer) _         As Integer      Declare Function GetMenuState Lib "user" (ByVal hMenu As Integer, _         ByVal wID As Integer, ByVal wFlags As Integer) As Integer      Declare Function GetSubMenu Lib "user" (ByVal hSubMenu As Integer, _         ByVal nPos As Integer) As Integer      Declare Function CheckMenuItem Lib "user" (ByVal hSubMenu As _         Integer, ByVal nPos As Integer, ByVal Flag As Integer) As Integer      Declare Function FindWindow Lib "user" (ByVal lpClassName As Any, _         ByVal lpCaption As Any) As Integer      Declare Function IsZoomed Lib "user" (ByVal hWnd As Integer) _         As Integer      ' Menu constants.      Const MF_BYPOSITION = &H400      Const MF_BYCOMMAND = &H0      Const MF_CHECK = &H8      Const MF_UNCHECKED = &H0      Const MyNull = 0&      Const ClassName = "OMain"						
  7. Type the following procedure:

          Function ChkMenuItem (TopLevel As Integer, SubLevel As Integer)         Dim ChWnd As Integer     ' Handle to the Microsoft Access window.         Dim hMenuTop As Integer  ' Handle to the Microsoft Access menu.         Dim hSubMenu As Integer  ' Handle to the sub menu.         Dim ItemID As Integer    ' Ordinal position of menu item.         ' If the form is maximized, the system menu is added to the forms         ' menu bar, so increment the actual TopLevel value by one.         If (IsZoomed(Screen.ActiveForm.hWnd)) Then            TopLevel = TopLevel + 1         End If         ' Assign the menu handles so the API         ' can find the items we are referring to...         ChWnd = FindWindow(ClassName, 0&)         hMenuTop = GetMenu(ChWnd)         hSubMenu = GetSubMenu(hMenuTop, TopLevel)         ' toggle based upon state of menu item...         Select Case GetMenuState(hSubMenu, SubLevel, MF_BYPOSITION)            Case MF_UNCHECKED               ChkMenuItem = CheckMenuItem(hSubMenu, SubLevel, _                                           MF_BYPOSITION Or MF_CHECK)            Case MF_CHECK               ChkMenuItem = CheckMenuItem(hSubMenu, SubLevel, _                                           MF_BYPOSITION Or MF_UNCHECKED)         End Select      End Function						
  8. Open the Menu Checking form in Form view. Note that the usual Microsoft Access menu disappears and is replaced by the custom menu you designed in these steps.

    Note that as you click Check Test on the Menu Check menu, a check mark toggles on and off next to the menu item.
References
For more information about custom menus, search for "customizing menus"using the Microsoft Access Help menu.

For more information about similar programming features, please see thefollowing articles in the Microsoft Knowledge Base:

88940 How to Dim (Gray) Menu Items with Access Basic

95935 How to Determine Whether a Menu Item Is Checked

Properties

Article ID: 90811 - Last Review: 10/26/2013 11:08:00 - Revision: 3.0

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • kbnosurvey kbarchive kbhowto kbprogramming KB90811
Feedback