ACC: How to Determine Whether a Menu Item Is CheckedID: Q95935
|
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.
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
0 1 2
File Edit View
---------------------------
0 Open Cut Form Design
1 Close Copy Form
2 Exit Paste Datasheet
Option Compare Database 'Use database order for string comparisons.
Option Explicit
Declare Function IsZoomed% Lib "User" (ByVal hWnd%)
Declare Function GetMenu% Lib "User" (ByVal hWnd%)
Declare Function GetSubMenu% Lib "User" (ByVal hMenu%, ByVal nPos%)
Declare Function GetMenuState% Lib "User" (ByVal hMenu%, ByVal idItem%,_
ByVal fuFlags%)
Declare Function GetActiveWindow% Lib "User" ()
Declare Function GetParent% Lib "User" (ByVal hwin%)
Declare Function GetClassName% Lib "User" (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 = "OMain"
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 = ""
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 = "OMain", 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
For more information about this topic, please see the following article
here in the Microsoft Knowledge Base:
Q90811 ACC: How to Check a Menu Item Using Access Basic
Additional query words: basic
Keywords : kbprg
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 18, 1999