ACC2000: How to Dim Menu Items or Disable Toolbar Buttons in VBA

This article was previously published under Q198464
This article has been archived. It is offered "as is" and will no longer be updated.
Novice: Requires knowledge of the user interface on single-user computers.

This article shows you how to dim or disable items on a menu bar, toolbar,or shortcut menu in Microsoft Access.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The CommandBars collection in Microsoft Access exposes all menu bars,toolbars, and shortcut menus in your application to Visual Basic forApplications so you can manipulate them programmatically. By using theproperties and methods of the CommandBars collection in your application,you can manipulate and customize both built-in and custom command bars.

NOTE: If you use Windows application programming interface (API) proceduresto manipulate menus in your Microsoft Access 2.0 or 7.0 database, you mustmodify the code to use the CommandBars object model in Visual Basic forApplications when you convert your database to Access 2000. WindowsAPI calls do not work with Microsoft Access 2000 menus, toolbars, orshortcut menus.

Each command bar, whether it is a menu bar, a toolbar, or a shortcut menu,consists of a collection of CommandBarControl objects. You can create threetypes of CommandBarControls on a command bar, each with its own methods andproperties:
  • CommandBarButton represents a button control on a command bar, which can display text, an icon, or both together.
  • CommandBarComboBox represents a custom edit box, drop-down list box, or combo box on a command bar.
  • CommandBarPopup represents a control on a command bar that displays another menu when you click it; the menu that appears is another command bar associated with the popup control.
You refer to a control on a command bar by its Caption property or Indexnumber. For example, if a control on the "Test" command bar has the caption"ClickMe" and an index value of 1, then the following two statements bothrefer to the same control:


The following example shows you how to disable or enable items on a menubar or toolbar based on events in your application.

NOTE: For this example to work in your own database, make sure that your database has a reference to the Microsoft Office 9.0 Object Library. Northwind.mdb has this reference set by default, but your database may not. To set a reference to the Microsoft Office 9.0 Object Library, click References on the Tools menu in the Visual Basic Editor, and then click to select the Microsoft Office 9.0 Object Library check box.
  1. Open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query in Design view, and save it as frmChangeBars.
  3. Enable the Control Wizards button on the Toolbox toolbar, and then add an option group control to the detail section of the form.
    1. In the "What label do you want for each option?" dialog box, type the following four labels, each in its own row: Dim View Menu, Dim Print Button, Dim New Database, and Enable All. Click Next.
    2. In the "Do you want one option to be the default choice?" dialog box, click "No, I don't want a default," and then click Next.
    3. In the "What value do you want to assign to each option?" dialog box, click Finish.
  4. Set the Name property of the option group control to CommandBarTest, and set the AfterUpdate property to the following event procedure:
    Private Sub CommandBarTest_AfterUpdate()   Dim CBarMenu as CommandBar, CBarTool as CommandBar   Dim CBarCtl as CommandBarPopup   ' Set the CommandBar objects to the menu bar and toolbar that   ' display when you open a form in Form view.   Set CBarMenu = CommandBars("NorthwindCustomMenuBar")   Set CBarTool = CommandBars("Form View")   ' Because the File menu is a Popup control, assign it to an object   ' variable so you can manipulate its CommandBar object.   Set CBarCtl = CBarMenu.Controls("File")   ' Program what happens when you click an option.   Select Case Me!CommandBarTest      ' You clicked Dim View Menu.      Case 1         CBarMenu.Controls("View").Enabled = False      ' You clicked Dim Print Button.      Case 2         CBarTool.Controls("Print...").Enabled = False      ' You clicked Dim New Database.      Case 3         CBarCtl.CommandBar.Controls("New...").Enabled = _                  False      ' You clicked Enable All.      Case 4         CBarMenu.Controls("View").Enabled = True         CBarTool.Controls("Print...").Enabled = True         CBarCtl.CommandBar.Controls("New...").Enabled = _                  True   End SelectEnd Sub					
  5. Open the frmChangeBars form in Form view.
  6. Click Dim View Menu and note that the View menu at the top of your screen dims.
  7. Click Dim Print Button and note that the Print button on the Form View toolbar dims.
  8. Click Dim New Database and note that the New Database selection on the File menu dims.
  9. Click Enable All to re-enable all three controls.
For more information about command bars, click Microsoft Access Help on theHelp menu, type "command bars" in the Office Assistant or the AnswerWizard, and then click Search to view the topics returned.

For additional information about creating and modifying command bars usingVisual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:
209974 ACC2000: How to Create Command Bars by Using Visual Basic Code

Article ID: 198464 - Last Review: 12/05/2015 10:13:36 - Revision: 3.2

Microsoft Access 2000 Standard Edition

  • kbnosurvey kbarchive kbhowto kbprogramming KB198464