Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you Check/Uncheck a menu item.

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
Using Access 2000

I have a menu bar named MMC_Edit. This menu bar contains the following items: File Edit View. Under View are 2 items: "Archived Data" and "Live Data". When the form is first opened I want to check the menu item "Live Data".

What is the syntax for this? (I know it's the state property, just don't know the syntax to get to it.)

I have tryed:

Dim cbc as object

Set cbc = CommandBars("MMC_Edit").Controls("View")

and have tryed many other combinations of this.

By the way, I dimensioned cbc as object rather than CommandBarControl, because Access won't accept CommandBarControls. Note that I have a reference to Microsoft Access 9.0 object library.
 
I know how to edit a main header on a menu:

Function CmdBarEnable(cmdBar As String)
On Error GoTo Cmd_Err
Dim Cbr, CbrCtl

Set Cbr = CommandBars("mnuLandManagement")
Set CbrCtl = Cbr.Controls(cmdBar)
CbrCtl.Enabled = True

Cmd_Exit:
Exit Function

Cmd_Err:
MsgBox Error$
Resume Cmd_Exit

End Function


However, I have never worked out how to 'drill-down' into the individual menu-items. I would like to know if/how this is possible though... James Goodman
 
Thanks James for responding. You verified that my syntax was correct. Therefore, I looked harder at my code and realized I was specifying the wrong menubar name (mnuEdit rather than MMC_Edit). (I had spent 2 hours messing with this before posting my problem, arrrgh.)

However, for others who want to check a menu item when the form is loaded, the following code should do it. (Note that when you set the properties of the menu item, it cannot have a picture (icon) associated with it.)

Menubar "mnuEdit" contains the following:
File Edit View

Under View is:
Live Data
Archive Data

This code will put a check mark by "Live Data"

Dim cbc as Object
Dim cbcItem as Object

Set cbc = CommandBars("mnuEdit").Controls("View")

For Each cbcItem In cbc.Controls
Debug.Print cbcItem.Caption
If (cbcItem.Caption = "&Live Data") Then 'The ampersand (&) is here because it is used to underline the "L" in Live.
cbcItem.State = -1
end if
Next cbcItem

If you want to uncheck the menuitem, set cbcItem.State=0.


If you want to check or uncheck an item when the user selects the menuitem, the OnAction event of each menuitem must call a macro or function. In the scenerio below, when the user selects "Archive Data", it will be checked and "Live Data" will be unchecked. Likewise, when "Live Data" is checked, "Archive Data" will be unchecked. The function below also assumes that there are other menuitems in the list besides "Archive Data" and "Live Data".

Function CheckUncheckMenuItem()

Dim cbc as Object
Dim cbcItem as Object

Set cbc = CommandBars.ActionControl 'Indicates item that was selected.

For Each cbcItem In cbc.Parent.Controls
If (cbcItem.Caption = "&Archived Table") Or (cbcItem.Caption = "&Live Table") Then
If (cbcItem.Caption = cbc.Caption) Then
cbcItem.State = -1
Else
cbcItem.State = 0
End If
End If
Next cbcItem

End Function


Still don't understand why I have to dimension the variables as Object rather than CommandBarControl. Anyone have any ideas? (I am referencing Microsoft Access 9.0 Object Library)
 
Now that I know my syntax was correct, thought about it some more and here is an easier way to put check marks beside the menuitems. (However, James, my previous post does show you how to iterate thru the menuitems.)

1. Put check mark to the left of "Live Data"

CommandBars("mnuEdit").Controls("View").Controls("Live Data").State = True

2. Put check mark to the left of the menuitem selected and unselect the other menuitem. (The OnAction event of the 2 menuitems should look like: =CheckUncheckMenuItem() )

Function CheckUncheckMenuItem()

Dim cbc as Object

Set cbc = CommandBars.ActionControl

cbc.State = True

If (cbc.Caption = "&Archived Data") Then
cbc.Parent.Controls("&Live Data").State = False
Else
cbc.Parent.Controls("&Archived Data").State = False
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top