Microsoft KB Archive/90811

= ACC: How to Check a Menu Item Using Access Basic =

Article ID: 90811

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 Q90811



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

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

NOTE: In Microsoft Access 7.0 and 97, you can use the SetMenuItem macro action to set the state of menu items (enabled or disabled, checked or cleared) on a custom menu bar or the global menu bar for the active window. For more information about the SetMenuItem action, search the Help Index for &quot;SetMenuItem Action.&quot;



MORE INFORMATION
The following Windows API function is used to check or clear a menu item.

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.

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 from menu items in the pop-up menu specified by the hMenu% parameter as follows:

  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) cannot have a check mark.

When determining the position of a menu command, separator bars count as commands. For example, to get to the Import menu 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.

Example
The example below creates a menu, associated with a form, that can be used to check or clear a menu command:

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

 Create a new blank form. 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. Save the form as Menu Checking.</li>  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 &quot;user&quot; (ByVal hWnd As Integer) _ As Integer Declare Function GetMenuState Lib &quot;user&quot; (ByVal hMenu As Integer, _        ByVal wID As Integer, ByVal wFlags As Integer) As Integer Declare Function GetSubMenu Lib &quot;user&quot; (ByVal hSubMenu As Integer, _        ByVal nPos As Integer) As Integer Declare Function CheckMenuItem Lib &quot;user&quot; (ByVal hSubMenu As _        Integer, ByVal nPos As Integer, ByVal Flag As Integer) As Integer Declare Function FindWindow Lib &quot;user&quot; (ByVal lpClassName As Any, _        ByVal lpCaption As Any) As Integer Declare Function IsZoomed Lib &quot;user&quot; (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 = &quot;OMain&quot; </li>  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 </li> 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.</li></ol>

<div class="references_section">