Microsoft KB Archive/160293: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 53: Line 53:
This article shows you how to dim or disable items on a menu bar, toolbar, or shortcut menu in Microsoft Access 97.<br />
This article shows you how to dim or disable items on a menu bar, toolbar, or shortcut menu in Microsoft Access 97.<br />
<br />
<br />
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 &quot;Building Applications with Microsoft Access 97&quot; manual.
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.


</div>
</div>
Line 69: Line 69:
* 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.
* 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 Index number. For example, if a control on the &quot;Test&quot; command bar has the caption &quot;ClickMe&quot; and an index value of 1, then the following two statements both refer to the same control:
You refer to a control on a command bar by its Caption property or Index number. 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 both refer to the same control:
<div class="indent">
<div class="indent">


CommandBars(&quot;Test&quot;).Controls(&quot;ClickMe&quot;)<br />
CommandBars("Test").Controls("ClickMe")<br />
CommandBars(&quot;Test&quot;).Controls(1)
CommandBars("Test").Controls(1)




Line 83: Line 83:
<li>Enable the Control Wizards button on the Toolbox toolbar, and then add an option group control to the detail section of the form.
<li>Enable the Control Wizards button on the Toolbox toolbar, and then add an option group control to the detail section of the form.
<ol style="list-style-type: lower-alpha;">
<ol style="list-style-type: lower-alpha;">
<li>In the &quot;What label do you want for each option?&quot; dialog box, type the following four labels, each in its own row: <span class="kbd userinput"> Dim View Menu</span>, <span class="kbd userinput"> Dim Print Button</span>, <span class="kbd userinput"> Dim New Database</span> and <span class="kbd userinput"> Enable All</span>. Click Next.</li>
<li>In the "What label do you want for each option?" dialog box, type the following four labels, each in its own row: <span class="kbd userinput"> Dim View Menu</span>, <span class="kbd userinput"> Dim Print Button</span>, <span class="kbd userinput"> Dim New Database</span> and <span class="kbd userinput"> Enable All</span>. Click Next.</li>
<li>In the &quot;Do you want one option to be the default choice?&quot; dialog box, click &quot;No, I don't want a default,&quot; and then click Next.</li>
<li>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.</li>
<li>In the &quot;What value do you want to assign to each option?&quot; dialog box, click Finish.</li></ol>
<li>In the "What value do you want to assign to each option?" dialog box, click Finish.</li></ol>
</li>
</li>
<li><p>Set the Name property of the option group control to CommandBarTest, and set the AfterUpdate property to the following event procedure:</p>
<li><p>Set the Name property of the option group control to CommandBarTest, and set the AfterUpdate property to the following event procedure:</p>
Line 93: Line 93:
         ' Set the CommandBar objects to the menu bar and toolbar that
         ' Set the CommandBar objects to the menu bar and toolbar that
         ' display when you open a form in Form view.
         ' display when you open a form in Form view.
         Set CBarMenu = CommandBars(&quot;NorthwindCustomMenuBar&quot;)
         Set CBarMenu = CommandBars("NorthwindCustomMenuBar")
         Set CBarTool = CommandBars(&quot;Form View&quot;)
         Set CBarTool = CommandBars("Form View")
         ' Because the File menu is a Popup control, assign it to an object
         ' Because the File menu is a Popup control, assign it to an object
         ' variable so you can manipulate its CommandBar object.
         ' variable so you can manipulate its CommandBar object.
         Set CBarCtl = CBarMenu.Controls(&quot;File&quot;)
         Set CBarCtl = CBarMenu.Controls("File")


         ' Program what happens when you click an option.
         ' Program what happens when you click an option.
Line 103: Line 103:
             ' You clicked Dim View Menu.
             ' You clicked Dim View Menu.
             Case 1
             Case 1
               CBarMenu.Controls(&quot;View&quot;).Enabled = False
               CBarMenu.Controls("View").Enabled = False


             ' You clicked Dim Print Button.
             ' You clicked Dim Print Button.
             Case 2
             Case 2
               CBarTool.Controls(&quot;Print...&quot;).Enabled = False
               CBarTool.Controls("Print...").Enabled = False


             ' You clicked Dim New Database.
             ' You clicked Dim New Database.
             Case 3
             Case 3
               CBarCtl.CommandBar.Controls(&quot;New Database...&quot;).Enabled = _
               CBarCtl.CommandBar.Controls("New Database...").Enabled = _
                   False
                   False


             ' You clicked Enable All.
             ' You clicked Enable All.
             Case 4
             Case 4
               CBarMenu.Controls(&quot;View&quot;).Enabled = True
               CBarMenu.Controls("View").Enabled = True
               CBarTool.Controls(&quot;Print...&quot;).Enabled = True
               CBarTool.Controls("Print...").Enabled = True
               CBarCtl.CommandBar.Controls(&quot;New Database...&quot;).Enabled = _
               CBarCtl.CommandBar.Controls("New Database...").Enabled = _
                   True
                   True
         End Select
         End Select
Line 135: Line 135:
== REFERENCES ==
== REFERENCES ==


For more information about command bars, search the Help Index for &quot;CommandBars collection,&quot; or ask the Microsoft Access 97 Office Assistant.<br />
For more information about command bars, search the Help Index for "CommandBars collection," or ask the Microsoft Access 97 Office Assistant.<br />
<br />
<br />
For more information about creating and modifying command bars using Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
For more information about creating and modifying command bars using Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

Latest revision as of 10:14, 21 July 2020

Knowledge Base


ACC97: How to Dim Menu Items or Disable Toolbar Buttons in Visual Basic for Applications

Article ID: 160293

Article Last Modified on 1/19/2007



APPLIES TO

  • Microsoft Access 97 Standard Edition



This article was previously published under Q160293

Moderate: Requires basic macro, coding, and interoperability skills.


SUMMARY

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

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 CommandBars collection in Microsoft Access 97 exposes all menu bars, toolbars, and shortcut menus in your application to Visual Basic for Applications so you can manipulate them programmatically. Using the properties 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) procedures to manipulate menus in your Microsoft Access 2.0 or 7.0 database, you must modify the code to use the CommandBars object model in Visual Basic for Applications when you convert your database to Microsoft Access 97. Windows API calls do not work with Microsoft Access 97 menus, toolbars, or shortcut 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 three types of CommandBarControls on a command bar, each with its own methods and properties:

  • 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 Index number. 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 both refer to the same control:

CommandBars("Test").Controls("ClickMe")
CommandBars("Test").Controls(1)


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

  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 Database...").Enabled = _
                      False
    
                ' You clicked Enable All.
                Case 4
                   CBarMenu.Controls("View").Enabled = True
                   CBarTool.Controls("Print...").Enabled = True
                   CBarCtl.CommandBar.Controls("New Database...").Enabled = _
                      True
             End Select
          End 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.


REFERENCES

For more information about command bars, search the Help Index for "CommandBars collection," or ask the Microsoft Access 97 Office Assistant.

For more information about creating and modifying command bars using Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

159692 ACC97: How to Create Command Bars Using Visual Basic Code


For more information on how to programmatically add and remove items on command bars, please see the following article(s) in the Microsoft Knowledge Base:

172300 ACC97: Command Bar Wizard Available in Download Center


Keywords: kbhowto kbprogramming kbusage KB160293