ACC: How to Check a Menu Item Using Access Basic
ID: Q90811
|
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.
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."
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.
- 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 "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"
- 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
- 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.
REFERENCES
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
|