Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Disable or Enable Items on a Custom Command Bar


View products that this article applies to.

This article was previously published under Q202308
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


Summary

This article describes methods that you can use to programmatically enable or disable all menu items or specific menu items on a custom command bar.

↑ Back to the top


More information

The examples in this article are based on the "NorthwindCustomMenuBar" command bar found in the sample database Northwind.mdb. The examples manipulate the CommandBar object, which is part of the Microsoft Office 9.0 Object Library. Therefore, the Microsoft Office 9.0 Object Library must be available on your computer; you must also set a reference to this library in the database in which you want to enable or disable menu items on your custom command bars.

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. To set a reference to the Microsoft Office 9.0 Object Library, follow these steps:
  1. In the Visual Basic Editor, on the Tools menu, click References.
  2. Locate the Microsoft Office 9.0 Object Library check box and click to select it; then click OK.
  3. On the File menu, click Close and Return to Microsoft Access. You do not have to save the module.

Disabling All Menu Items on a Custom Command Bar Example

The following example disables all menu items on a custom command bar:
  1. Open your database.
  2. In the Database window, click Modules under Objects, and then click New.
  3. Type or paste the following code into the module:
    Public Function AllowMenus(CmdBarName As String, CmdbarEnabled As _
       Boolean)
    
       '============================================================='
       'This function has two arguments: CmdbarName is a string that '
       'passes the name of the command bar that the code enables or  '
       'disables. CmdBarEnabled is a Boolean value in which you pass '
       '"True" or "False" in order to enable or disable the command  '
       'bar being modified.                                          '
       '                                                             '
       'Example: To disable the command bar "NorthwindCustomMenuBar" '
       'in the Northwind sample database, use the following:         '
       '                                                             '
       'AllowMenus("NorthwindCustomMenuBar",False)                   '
       '============================================================='
    
       Dim Cmdbar As CommandBar, Cbct As CommandBarControl
    
       On Error GoTo Err_AllowMenus
       Set Cmdbar = CommandBars(CmdBarName)
    
       If Cmdbar.Visible = False Then Cmdbar.Visible = True
    
          For Each Cbct In Cmdbar.Controls
             Cbct.Enabled = CmdbarEnabled
          Next Cbct
    
       Exit_AllowMenus:
          Exit Function
    
       Err_AllowMenus:
       MsgBox "Error " & CStr(Err) & " " & Err.Description & _
          " has occurred in the AllowMenus Function", vbOKOnly, _
          "Error Detected"
       Resume Exit_AllowMenus
    
    End Function
    					
  4. Save the module.
  5. Press CTRL+G to open the Immediate window and test the function. For example, if you are using the Northwind sample database, type the following line, and then press ENTER:
    ?AllowMenus("NorthwindCustomMenuBar", False)
    					

Enabling or Disabling Specific Menu Items on a Command Bar Example

The following sample function allows you to programmatically enable or disable a specific menu item on a command bar, rather than enable or disable the entire command bar.

  1. Open your database.
  2. In the Database window, click Modules under Objects, and then click New.
  3. Type or paste the following code into the module:
    Public Function CommandbarEnable(Cmdbar As CommandBar, _
       CmdbarEnabled As Boolean, TopLevel As Integer, _
       Optional Sublevel As Integer)
    
      '================================================================='
      'This function has four arguments:                                '
      '                                                                 '
      'Cmdbar is a CommmandBar object that represents the command       '
      'bar containing the menu item to be enabled or disabled.          '
      '                                                                 '
      'CmdBarEnabled is a Boolean value in which you pass "True"        '
      'or "False" in order to enable or disable the menu item being     '
      'manipulated.                                                     '
      '                                                                 '
      'TopLevel is an integer representing the index of the Top-level   '
      'menu item being manipulated.                                     '
      '                                                                 '
      'Sublevel is an optional integer representing the index of the    '
      'menu item being manipulated under the Top-level menu item.       '
      '                                                                 '
      'Example: To disable only the "File" menu item on the             '
      '"NorthwindCustomMenuBar" command bar, use the following:         '
      '                                                                 '
      'CommandbarEnable(Commandbars("NorthwindCustomMenuBar"),False,1)  '
      '                                                                 '
      'Example2: To disable the "Get External Data" Menu item under     '
      'the "File" menu item on the "NorthwindCustomMenuBar" command     '
      'bar, use the following:                                          '
      '                                                                 '
      'CommandbarEnable(Commandbars("NorthwindCustomMenuBar"),False,1,3)'
      '                                                                 '
      'To "re-enable" the same menu item, use the following:            '
      '                                                                 '
      'CommandbarEnable(Commandbars("NorthwindCustomMenuBar"),True,1,3) '
      '================================================================='
    
       Dim SubCommandbar
    
       On Error GoTo Err_CommandBarEnable
    
       'If the command bar is not visible, make it so.
       If Cmdbar.Visible = False Then Cmdbar.Visible = True
    
       'If no menu item on a submenu is selected for enabling\disabling,
       'enable\disable the top level menu choice only.
          If IsMissing(Sublevel) Or Sublevel = 0 Then
             Cmdbar.Controls(TopLevel).Enabled = CmdbarEnabled
             'If a menu item on a submenu is selected for
             'enabling\disabling, do so now.
          Else
             Set SubCommandbar = Cmdbar.Controls(TopLevel)
             SubCommandbar.Controls(Sublevel).Enabled = CmdbarEnabled
          End If
    
    Exit_CommandBarEnable:
          Exit Function
    
    Err_CommandBarEnable:
       MsgBox "Error " & CStr(Err) & " " & Err.Description & _
          " has occurred in the CommandBarEnable Function", vbOKOnly, _
          "Error Detected"
       Resume Exit_CommandBarEnable
    
    End Function
    					
  4. Save the module.
  5. Press CTRL+G to open the Immediate window and test the function. For example, if you are using the Northwind sample database, type the following line, and then press ENTER:
    ?CommandbarEnable(Commandbars("NorthwindCustomMenuBar"),False,1,3)
    					

↑ Back to the top


References

For more information about the CommandBar object, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type CommandBar object in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about creating custom command bars, click Microsoft Access Help on the Help menu, type work with menu bars in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB202308, kbprogramming, kbofficeprog, kbhowto, kbdta

↑ Back to the top

Article Info
Article ID : 202308
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 415