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

ID: Q202308


The information in this article applies to:
  • Microsoft Access 2000

Advanced: Requires expert coding, interoperability, and multiuser skills.

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


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.


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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
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:


  4. 
    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 
  5. Save the module.


  6. 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:


  7. 
    ?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:


  4. 
    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 
  5. Save the module.


  6. 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:


  7. 
    ?CommandbarEnable(Commandbars("NorthwindCustomMenuBar"),False,1,3) 


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.

Additional query words: enabling disabling inf toolbar toolbars commandbar

Keywords : kbdta AccCon UifCmdbar
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 13, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.