Microsoft KB Archive/209974: Difference between revisions
(importing KB archive) |
m (Text replacement - "&" to "&") |
||
Line 125: | Line 125: | ||
Exit Sub | Exit Sub | ||
AddNewCB_Err: | AddNewCB_Err: | ||
MsgBox "Error " & | MsgBox "Error " & Err.Number & vbCr & Err.Description | ||
Exit Sub | Exit Sub | ||
End Sub | End Sub | ||
Line 140: | Line 140: | ||
Exit Function | Exit Function | ||
ToggleButton_Err: | ToggleButton_Err: | ||
MsgBox "Error " & | MsgBox "Error " & Err.Number & vbCr & Err.Description | ||
Exit Function | Exit Function | ||
End Function | End Function | ||
Line 177: | Line 177: | ||
Exit Function | Exit Function | ||
Else | Else | ||
MsgBox "Error " & | MsgBox "Error " & Err.Number & vbCr & Err.Description | ||
Exit Function | Exit Function | ||
End If | End If | ||
Line 215: | Line 215: | ||
' Create a popup control on the bar and set its caption. | ' Create a popup control on the bar and set its caption. | ||
Set MBarCtl = MBar.Controls.Add(Type:=msoControlPopup) | Set MBarCtl = MBar.Controls.Add(Type:=msoControlPopup) | ||
MBarCtl.Caption = "Displa& | MBarCtl.Caption = "Displa&y" | ||
' Create 2 controls on the Display popup and set some properties. | ' Create 2 controls on the Display popup and set some properties. | ||
Line 221: | Line 221: | ||
With MBarSubCtl | With MBarSubCtl | ||
.Style = msoButtonIconAndCaption | .Style = msoButtonIconAndCaption | ||
.Caption = "E& | .Caption = "E&nable ClickMe" | ||
.FaceId = 59 | .FaceId = 59 | ||
.OnAction = "=ToggleClickMe()" | .OnAction = "=ToggleClickMe()" | ||
Line 230: | Line 230: | ||
With MBarSubCtl | With MBarSubCtl | ||
.Style = msoButtonIconAndCaption | .Style = msoButtonIconAndCaption | ||
.Caption = "Di& | .Caption = "Di&sable ClickMe" | ||
.FaceId = 276 | .FaceId = 276 | ||
.OnAction = "=ToggleClickMe()" | .OnAction = "=ToggleClickMe()" | ||
Line 241: | Line 241: | ||
With MBarCtl | With MBarCtl | ||
.BeginGroup = True | .BeginGroup = True | ||
.Caption = "& | .Caption = "&ClickMe" | ||
.Style = msoButtonCaption | .Style = msoButtonCaption | ||
.OnAction = "=MsgBox(""You clicked ClickMe"")" | .OnAction = "=MsgBox(""You clicked ClickMe"")" | ||
Line 251: | Line 251: | ||
With MBarCtl | With MBarCtl | ||
.BeginGroup = True | .BeginGroup = True | ||
.Caption = "& | .Caption = "&Show Northwind Menu Bar" | ||
.Style = msoButtonCaption | .Style = msoButtonCaption | ||
.OnAction = "=SampleMenuDisable()" | .OnAction = "=SampleMenuDisable()" | ||
Line 257: | Line 257: | ||
Exit Sub | Exit Sub | ||
AddNewMB_Err: | AddNewMB_Err: | ||
MsgBox "Error " & | MsgBox "Error " & Err.Number & vbCr & Err.Description | ||
Exit Sub | Exit Sub | ||
End Sub | End Sub | ||
Line 284: | Line 284: | ||
Exit Function | Exit Function | ||
ToggleClickMe_Err: | ToggleClickMe_Err: | ||
MsgBox "Error " & | MsgBox "Error " & Err.Number & vbCr & Err.Description | ||
Exit Function | Exit Function | ||
End Function | End Function |
Revision as of 12:32, 21 July 2020
Article ID: 209974
Article Last Modified on 10/11/2006
APPLIES TO
- Microsoft Access 2000 Standard Edition
This article was previously published under Q209974
This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
Advanced: Requires expert coding, interoperability, and multiuser skills.
IN THIS TASK
SUMMARY
You can programmatically create toolbars and menu bars, as well as add and remove items from those bars. This article demonstrates how to do this.
back to the top
Set References to Use Command Bars
In Microsoft Access 2000, toolbars, shortcut menus, and menu bars are combined into a single CommandBars collection that you can control programmatically by using Visual Basic for Applications. You can modify any Microsoft Access built-in menu bars or toolbars, and you can create and modify custom toolbars, menu bars, and shortcut menus.
In order to program with command bars, you must set a reference to the Microsoft Office 9.0 Object Library. To do so follow these steps:
- While in the Visual Basic Editor, on the Tools menu, click References.
- In the References dialog box, click to select Microsoft Office 9.0 Object Library.
- 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 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 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:="Sample Toolbar", 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 = "Button" .Style = msoButtonCaption .TooltipText = "Display Message Box" .OnAction = "=MsgBox(""You pressed a toolbar button!"")" 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 = "Toggle Button" .TooltipText = "Toggle First Button" .OnAction = "=ToggleButton()" End With ' Create a combo box control on the bar and set some properties. Set CBarCtl = CBar.Controls.Add(msoControlComboBox) With CBarCtl .Caption = "Drop Down" .Width = 100 .AddItem "Create Button", 1 .AddItem "Remove Button", 2 .DropDownWidth = 100 .OnAction = "=AddRemoveButton()" End With Exit Sub AddNewCB_Err: MsgBox "Error " & 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("Sample Toolbar").Controls(1) CBButton.Visible = Not CBButton.Visible Exit Function ToggleButton_Err: MsgBox "Error " & 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("Sample Toolbar") 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 = "New Button" .Style = msoButtonCaption .BeginGroup = True .Tag = "New Button" .OnAction = "=MsgBox(""This is a new button!"")" End With ' Find and remove the new button if it exists. Case 2 Set CBNewButton = CBar.FindControl(Tag:="New Button") CBNewButton.Delete End Select Exit Function AddRemoveButton_Err: ' If the button does not exist. If Err.Number = 91 Then MsgBox "Cannot remove button that does not exist!" Exit Function Else MsgBox "Error " & 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
- Switch back to the Access window and you will see a floating toolbar on the screen.
- Click the word "Button" on the toolbar and note the message box that appears.
- Click the button with the light bulb image and note that "Button" disappears. Click it again and "Button" reappears.
- Select Create Button in the combo box and note that a button called New Button is added to the toolbar.
- Select Remove Button in the combo box and note that New Button is removed from the toolbar.
Example 2: Creating a Menu Bar
- Open the sample database Northwind.mdb.
Create a module and 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:="Sample Menu Bar", 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 = "Displa&y" ' Create 2 controls on the Display popup and set some properties. Set MBarSubCtl = MBarCtl.Controls.Add(Type:=msoControlButton) With MBarSubCtl .Style = msoButtonIconAndCaption .Caption = "E&nable ClickMe" .FaceId = 59 .OnAction = "=ToggleClickMe()" .Parameter = 1 .BeginGroup = True End With Set MBarSubCtl = MBarCtl.Controls.Add(Type:=msoControlButton) With MBarSubCtl .Style = msoButtonIconAndCaption .Caption = "Di&sable ClickMe" .FaceId = 276 .OnAction = "=ToggleClickMe()" .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 = "&ClickMe" .Style = msoButtonCaption .OnAction = "=MsgBox(""You clicked ClickMe"")" 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 = "&Show Northwind Menu Bar" .Style = msoButtonCaption .OnAction = "=SampleMenuDisable()" End With Exit Sub AddNewMB_Err: MsgBox "Error " & 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("Sample Menu Bar") 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 "Error " & 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("Sample Menu Bar").Visible = False Application.CommandBars("NorthwindCustomMenuBar").Visible = True End Function
To test this function, type the following line in the Immediate window, and then press ENTER:
AddNewMB
- Switch back to the Access window and you will see a docked menu bar on the left side of the screen.
- Click the ClickMe button on the menu and note the message box that appears.
- Click the Display button, and then click Disable ClickMe. Note that the ClickMe button is dimmed.
- Click the Display button, and then click Enable ClickMe. Note that the ClickMe button is enabled.
- Click the Show Northwind Menu Bar button to restore the original system menu bar.
REFERENCES
This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:
233324 ACC2000: Microsoft Access 2000 Sample Forms Database Available in Download Center
For more information on how to programmatically add and remove items on command bars, please see the following article in the Microsoft Knowledge Base:
172300 ACC97: Command Bar Wizard Available in Download Center
Keywords: kbhowto kbhowtomaster kbprogramming kbusage KB209974