Microsoft KB Archive/306369

= How to create command bars by using Visual Basic code in Access 2002 =

Article ID: 306369

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q306369



For a Microsoft Access 2000 version of this article, see 209974.

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

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

IN THIS TASK
SUMMARY
 * Set References to Use Command Bars
 * Example 1: Creating a Toolbar
 * Example 2: Creating a Menu Bar

REFERENCES



SUMMARY
You can programmatically create toolbars and menu bars. You can also programmatically add and remove items from those toolbars and menu bars. This article demonstrates how to do this.

Set References to Use Command Bars
In Microsoft Access 2002, toolbars, shortcut menus, and menu bars are combined into a single CommandBars collection. You can control the CommandBars collection programmatically by using Visual Basic for Applications. You can modify any Microsoft Access built-in menu bar or toolbar. You can also create and modify custom toolbars, menu bars, and shortcut menus.

To program with command bars, you must set a reference to the Microsoft Office 10.0 Object Library. To do so follow these steps:
 * 1) In the Visual Basic Editor, click References on the Tools menu.
 * 2) In the References dialog box, select Microsoft Office 10.0 Object Library.
 * 3) Click OK.

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 following examples create command bars programmatically and they assign actions to their controls. Example 1 creates a floating toolbar. Example 2 creates a docked menu bar.

back to the top

Example 1: Creating a Toolbar
 Open the sample database Northwind.mdb.  Create a module, and then type the following procedures: '**************************************************************** ' This procedure creates a new toolbar. '**************************************************************** Sub AddNewCB Dim CBar As CommandBar, CBarCtl As CommandBarControl On Error GoTo AddNewCB_Err

' Create a new floating toolbar and make it visible. Set CBar = CommandBars.Add(Name:=&quot;Sample Toolbar&quot;, Position:= _     msoBarFloating) CBar.Visible = True

' Create a button with text on the bar and set some properties. Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton) With CBarCtl .Caption = &quot;Button&quot; .Style = msoButtonCaption .TooltipText = &quot;Display Message Box&quot; .OnAction = &quot;=MsgBox(&quot;&quot;You pressed a toolbar button!&quot;&quot;)&quot; End With

' Create a button with an image on the bar and set some ' properties. Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton) With CBarCtl .FaceId = 1000 .Caption = &quot;Toggle Button&quot; .TooltipText = &quot;Toggle First Button&quot; .OnAction = &quot;=ToggleButton&quot; End With

' Create a combo box control on the bar and set some properties. Set CBarCtl = CBar.Controls.Add(msoControlComboBox) With CBarCtl .Caption = &quot;Drop Down&quot; .Width = 100 .AddItem &quot;Create Button&quot;, 1 .AddItem &quot;Remove Button&quot;, 2 .DropDownWidth = 100 .OnAction = &quot;=AddRemoveButton&quot; End With Exit Sub AddNewCB_Err: MsgBox &quot;Error &quot; & Err.Number & vbCr & Err.Description Exit Sub End Sub

'**************************************************************** ' This procedure is called from a button on the toolbar. ' It toggles the Visible property of another button on the bar. '**************************************************************** Function ToggleButton Dim CBButton As CommandBarControl On Error GoTo ToggleButton_Err Set CBButton = CommandBars(&quot;Sample Toolbar&quot;).Controls(1) CBButton.Visible = Not CBButton.Visible Exit Function ToggleButton_Err: MsgBox &quot;Error &quot; & Err.Number & vbCr & Err.Description Exit Function End Function

'**************************************************************** 'This procedure is called from a combo box on the toolbar 'It adds a button to the bar or removes it '**************************************************************** Function AddRemoveButton Dim CBar As CommandBar, CBCombo As CommandBarComboBox Dim CBNewButton As CommandBarButton On Error GoTo AddRemoveButton_Err Set CBar = CommandBars(&quot;Sample Toolbar&quot;) Set CBCombo = CBar.Controls(3) Select Case CBCombo.ListIndex ' If Create Button is selected, create a button on the bar Case 1 Set CBNewButton = CBar.Controls.Add(Type:=msoControlButton) With CBNewButton .Caption = &quot;New Button&quot; .Style = msoButtonCaption .BeginGroup = True .Tag = &quot;New Button&quot; .OnAction = &quot;=MsgBox(&quot;&quot;This is a new button!&quot;&quot;)&quot; End With ' Find and remove the new button if it exists. Case 2 Set CBNewButton = CBar.FindControl(Tag:=&quot;New Button&quot;) CBNewButton.Delete End Select Exit Function AddRemoveButton_Err: ' If the button does not exist. If Err.Number = 91 Then MsgBox &quot;Cannot remove button that does not exist!&quot; Exit Function Else MsgBox &quot;Error &quot; & Err.Number & vbCr & Err.Description Exit Function End If End Function   To test this function, type the following line in the Immediate window, and then press ENTER: AddNewCB  Return to Access. Note that there is a floating toolbar on the screen. Click the word &quot;Button&quot; on the toolbar. Note the message box that appears. Click the button with the light bulb image. Note that &quot;Button&quot; disappears. Click it again. Note that &quot;Button&quot; reappears. Select Create Button in the combo box. Note that a button named New Button is added to the toolbar.</li> Select Remove Button in the combo box. Note that New Button is removed from the toolbar.</li></ol>

Example 2: Creating a Menu Bar
<ol> Open the sample database Northwind.mdb.</li>  Create a module, and then type the following procedures: '**************************************************************** ' This procedure creates a new menu bar. '**************************************************************** Sub AddNewMB Dim MBar As CommandBar, MBarCtl As CommandBarControl Dim MBarPopup As CommandBarPopup, MBarSubCtl as CommandBarControl On Error GoTo AddNewMB_Err

' Create a new menu bar and dock it on the left. Set MBar = CommandBars.Add(Name:=&quot;Sample Menu Bar&quot;, Position:= _     msoBarLeft, MenuBar:=True, Temporary:=False) ' Make the menu bar visible. MBar.Visible = True ' Prevent users from undocking the menu bar. MBar.Protection = msoBarNoMove

' Create a popup control on the bar and set its caption. Set MBarCtl = MBar.Controls.Add(Type:=msoControlPopup) MBarCtl.Caption = &quot;Displa&y&quot;

' Create 2 controls on the Display popup and set some properties. Set MBarSubCtl = MBarCtl.Controls.Add(Type:=msoControlButton) With MBarSubCtl .Style = msoButtonIconAndCaption .Caption = &quot;E&nable ClickMe&quot; .FaceId = 59 .OnAction = &quot;=ToggleClickMe&quot; .Parameter = 1 .BeginGroup = True End With Set MBarSubCtl = MBarCtl.Controls.Add(Type:=msoControlButton) With MBarSubCtl .Style = msoButtonIconAndCaption .Caption = &quot;Di&sable ClickMe&quot; .FaceId = 276 .OnAction = &quot;=ToggleClickMe&quot; .Parameter = 2 .BeginGroup = True End With

' Add another control to the menu bar. Set MBarCtl = MBar.Controls.Add(Type:=msoControlButton) With MBarCtl .BeginGroup = True .Caption = &quot;&ClickMe&quot; .Style = msoButtonCaption .OnAction = &quot;=MsgBox(&quot;&quot;You clicked ClickMe&quot;&quot;)&quot; End With

' Add a control to make this menu bar invisible and bring back ' the system menu bar. Set MBarCtl = MBar.Controls.Add(Type:=msoControlButton) With MBarCtl .BeginGroup = True .Caption = &quot;&Show Northwind Menu Bar&quot; .Style = msoButtonCaption .OnAction = &quot;=SampleMenuDisable&quot; End With Exit Sub AddNewMB_Err: MsgBox &quot;Error &quot; & Err.Number & vbCr & Err.Description Exit Sub End Sub

'**************************************************************** ' This procedure uses the Parameter property of a command bar ' control to execute a different action depending on which item ' you click on a popup menu. '**************************************************************** Function ToggleClickMe Dim MyMenu As CommandBar Dim MBarClickMe As CommandBarControl On Error GoTo ToggleClickMe_Err Set MyMenu = CommandBars(&quot;Sample Menu Bar&quot;) Set MBarClickMe = MyMenu.Controls(2) ' The ActionControl property of command bars returns the control ' whose OnAction property is running this procedure. With CommandBars.ActionControl Select Case .Parameter Case 1 MBarClickMe.Enabled = True Case 2 MBarClickMe.Enabled = False End Select End With Exit Function ToggleClickMe_Err: MsgBox &quot;Error &quot; & Err.Number & vbCr & Err.Description Exit Function End Function

'**************************************************************** ' This function restores the original menu bar. Because there can ' only be one system menu bar, you must hide the sample menu bar ' when you want to bring back the previous system menu bar. '**************************************************************** Function SampleMenuDisable Application.CommandBars(&quot;Sample Menu Bar&quot;).Visible = False Application.CommandBars(&quot;Menu Bar&quot;).Visible = True End Function </li>  To test this function, type the following line in the Immediate window, and then press ENTER: AddNewMB </li> Return to Access. Note that there is a docked menu bar to the left on the screen.</li> Click the ClickMe button on the menu. Note the message box that appears.</li> Click the Display button, and then click Disable ClickMe. Note that the ClickMe button is no longer available (is dimmed).</li> Click the Display button, and then click Enable ClickMe. Note that the ClickMe button is enabled.</li> Click the Show Northwind Menu Bar button to restore the original system menu bar.</li></ol>

back to the top

<div class="references_section">