ACC: How to Check a Menu Item Using Access BasicID: Q90811
|
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 "SetMenuItem Action."
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
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.
Macro Name Action
------------------------------
&Check Test RunCode
&Check Test Actions
----------------------------------
RunCode
Function Name: ChkMenuItem(0,0)
Macro Name Action
---------------------
Custom Menu AddMenu
Custom Menu Actions
---------------------------------------
AddMenu
Menu Name: &Menu Check
Menu Macro Name: Menu Checking
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"
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
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 the
following articles in the Microsoft Knowledge Base:
Q88940 How to Dim (Gray) Menu Items with Access Basic
Q95935 How to Determine Whether a Menu Item Is Checked
Keywords : kbprg
Version : 1.0 1.10 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: March 10, 1999