ACC: How to Determine Whether a Menu Item Is Checked
ID: Q95935
|
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.
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:
- Copy the Access Basic code at the bottom of this article to a new
module.
- Open the Customers form in Design view.
- Select the Customer ID text box.
- Specify the following for the OnDblClick property in the property
sheet:
OnDblClick: =MsgBox(Str(IsMenuChecked(2,2)))
- Switch to Form view. Note that Datasheet on the View menu is not checked
because the form is not in Datasheet view.
- Double-click the Customer ID text box. Note that a zero appears
indicating that the Datasheet item on the View menu is not checked.
- Switch to Datasheet view. Note that the Datasheet item on the View menu
is now checked because the form is in Datasheet view.
- 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 "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
REFERENCES
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