Microsoft KB Archive/95935

= ACC: How to Determine Whether a Menu Item Is Checked =

Article ID: 95935

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 Q95935



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

This article discusses how you can make application programming interface (API) calls to determine whether or not a menu item is checked (selected). This method can be used, for example, when you want to determine whether a form is in Form view or Datasheet view. If a form is in Datasheet view, the Datasheet menu item on the View menu is checked. Thus, determining whether this menu item is checked allows you to determine whether the form is in Datasheet view.

The program code in this article comes from the ORDENTRY.MDB sample database and is presented in this article in a condensed version.



MORE INFORMATION
To determine whether a menu item is selected, you can use the IsMenuChecked function listed in the code at the end of this article. IsMenuChecked accepts two parameters indicating the position of the desired menu item; IsMenuChecked returns true if the item is selected and false if it is not.

To find the position of the menu item you want to select, begin at 0 (zero) and count from the left-most menu item until you reach the item you want. This determines the first number. Then, from the selected menu, begin with zero and count down the submenu until you reach the submenu item you want. This determines the second number. (NOTE: separator bars are considered submenu items, and always return a True from the IsMenuChecked function.)

For example, suppose you have the following menus:

  File   Edit    View --  Open   Cut     Form Design Close Copy    Form Exit  Paste   Datasheet

To determine whether Datasheet is checked, you would call IsMenuChecked with the values 2 and 2, because the View menu is in position 2 and the Datasheet item on the View submenu is in position 2. Note the same menu with numbered rows and columns to illustrate this further:

        0       1      2 File    Edit   View ---   0  Open     Cut    Form Design 1 Close    Copy   Form 2 Exit     Paste  Datasheet

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.

For an example of how to use this function, follow these steps in the sample database NWIND.MDB:


 * 1) Copy the Access Basic code at the bottom of this article to a new module.
 * 2) Open the Customers form in Design view.
 * 3) Select the Customer ID text box.
 * 4) Specify the following for the OnDblClick property in the property sheet:

OnDblClick: =MsgBox(Str(IsMenuChecked(2,2)))
 * 1) Switch to Form view. Note that Datasheet on the View menu is not checked because the form is not in Datasheet view.
 * 2) Double-click the Customer ID text box. Note that a zero appears indicating that the Datasheet item on the View menu is not checked.
 * 3) Switch to Datasheet view. Note that the Datasheet item on the View menu is now checked because the form is in Datasheet view.
 * 4) Double-click the Customer ID text box. Note that this time a -1 appears, indicating that the Datasheet item on the View menu is checked.

The following is the code for the sample function IsMenuChecked.

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 Compare Database   'Use database order for string comparisons. Option Explicit

Declare Function IsZoomed% Lib &quot;User&quot; (ByVal hWnd%) Declare Function GetMenu% Lib &quot;User&quot; (ByVal hWnd%) Declare Function GetSubMenu% Lib &quot;User&quot; (ByVal hMenu%, ByVal nPos%) Declare Function GetMenuState% Lib &quot;User&quot; (ByVal hMenu%, ByVal idItem%,_                      ByVal fuFlags%) Declare Function GetActiveWindow% Lib &quot;User&quot; Declare Function GetParent% Lib &quot;User&quot; (ByVal hwin%) Declare Function GetClassName% Lib &quot;User&quot; (ByVal hwin%, ByVal stBuf$,_                      ByVal cch%)

Const WU_MF_BYPOSITION = &H400 Const WU_MF_CHECKED = &H8 ' OMain is the predefined classname of the Global Const WU_WC_ACCESS = &quot;OMain&quot;

Function IsMenuChecked (iMenu%, iItem%) As Integer Dim hMainMenu% Dim hMenu% Dim Flags%

'--     'If the window is maximized, there is an additional 'Control menu added to the MDI window. '--     If (IsZoomed(Screen.ActiveForm.hWnd)) Then iMenu% = iMenu% + 1 End If

'--     ' Get the appropriate handle to the menu item. '--     hMainMenu% = GetMenu(GetAccessHwnd) hMenu% = GetSubMenu(hMainMenu%, iMenu%) Flags% = WU_MF_BYPOSITION Or WU_MF_CHECKED

'--     ' Call the API that returns the state of the menu. '--     IsMenuChecked = (GetMenuState(hMenu%, iItem%, Flags%) <> 0) End Function

Function StWindowClass (hWnd As Integer) As String Const cchMax = 255 Dim cch% Dim stBuff As String * cchMax

'--     ' Get the class name of the window relating to hWnd ' and return this as the function. '--     cch% = GetClassName(hWnd, stBuff, cchMax) If (hWnd% = 0) Then StWindowClass = &quot;&quot; Else StWindowClass = (Left$(stBuff, cch%)) End If  End Function

Function GetAccessHwnd As Integer Dim hWnd%

'--     ' Keep getting the handle of the parent, until the ' ClassName = &quot;OMain&quot;, which is the class name of     ' the Microsoft Access window. '--     hWnd% = GetActiveWindow While ((StWindowClass(hWnd%) <> WU_WC_ACCESS) And (hWnd% <> 0)) hWnd% = GetParent(hWnd%) Wend GetAccessHwnd = hWnd% End Function

