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

ID: Q177361


The information in this article applies to:


SUMMARY

Advanced: Requires expert coding, interoperability, and multi-user skills.

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.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access 97" manual.


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 8.0 Object Library. Therefore, the Microsoft Office 8.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.

To set a reference to the Microsoft Office 8.0 Object Library, follow these steps:

  1. Open your database.


  2. In the Database window, click the Modules tab.


  3. Select any module and click Design, or click New if no modules appear on the Modules tab in the Database window.


  4. On the Tools menu, click References.


  5. Locate the Microsoft Office 8.0 Object Library check box and click to select it; then click OK.


  6. On the File menu, click Close. It is not necessary 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 the Modules tab, 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 Debug 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 the entire command bar itself.
  1. Open your database.


  2. In the Database window, click the Modules tab, 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 commmand 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 Debug 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)



REFERENCES

For more information about the CommandBar object, search the Help Index for "CommandBar Object."

For more information about creating custom command bars, ask the Office Assistant for help on how to "Create a custom toolbar for the current database," or use the Command Bar Wizard.

For more information about obtaining the Command Bar Wizard, please see the following article in the Microsoft Knowledge Base:

Q172300 ACC97: Command Bar Wizard Available on MSL

Additional query words: enabling disabling inf toolbar toolbars commandbar


Keywords          : UifCmdbar 
Version           : WINDOWS:97
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 22, 1999