Private Sub FormOptions
End Sub
Step 2: Define your CommandBar
Dim cbr as CommandBar
Dim cbrButton As CommandBarButton
Dim cbrcombo As CommandBarComboBox
Dim cbrEdit As CommandBarControl
'or
Dim cbr as Object 'Commandbar
Dim cbrButton As Object 'CommandBarButton
Dim cbrcombo As Object 'CommandBarComboBox
Dim cbrEdit As Object 'CommandBarControl
Note: The second method shown above is used when implementing late binding.
set cbr = CommandBars.Add(Name, [Position], [Menubar], [Temporary])
1. The first argument, [Name], of the CommandBars.Add method is the name you will use to refer to this CommandBar throughout your application.
Set ctrl = cbr.Controls.Add([Type], [ID], [Parameter], [Before], [Temporary])
1. The [Type] argument can contain any of the msoControl constants (msoControlButton, msoControlEdit, msoControlDropdown, msoControlComboBox, or msoControlPopup). Use the numeric values or create your own constants if you intend to institute late binding.
Debug.print CommandBars(commandbarname).controls(controlcaption).ID
example:
Debug.print CommandBars("Database Table/Query").controls("&Print...").ID
If you don’t know the CommandBar name, or the precise caption of the control, use the following subroutine to print a list of the controls (and their associated CommandBars) which have a caption similar to what you are looking for.
Public Sub FindID(CaptionContains As String)
Dim cbr As CommandBar
Dim ctrl As CommandBarControl
For Each cbr In Application.CommandBars
For Each ctrl In cbr.Controls
'Because many of the controls contain keyboard shortcuts, they
'have ampersands embedded within the caption that must be
'removed before doing the comparison to the search string.
If InStr(Replace(ctrl.Caption, "&", ""), CaptionContains) > 0 Then
Debug.Print cbr.Name, ctrl.Caption, ctrl.Id
End If
Next
Next
End Sub
Note: I found 50 CommandBar/control combinations that contain the caption “Print” (the ID for “&Print…” is 4, the ID for “Print Pre&view” is 109).
Ctrl.Caption = “&Active Only”
If the control is a button, the caption is displayed on the button. If the control is a combo box or textbox, the caption will display to the left of the actual control, as though it were the label associated with the control. The other purpose for the caption property is to use it to refer to the control object, as in:
set ctrl = cbr.controls(“&Active Only”)
2. The purpose of shortcut menus is to simplify tasks, but it is left up to you (the developer) to define what you want to accomplish when a shortcut menu button is clicked, a combo box item is selected, or text is entered in a textbox. You do this by defining the "OnAction" property of each of the controls. If you fail to define the OnAction property of a button, clicking it will will have no effect. If you fail to define the OnAction property of a combo box or textbox, then selecting an item from the combo box or entering text into a text box will update the “value” of the control, but will not perform any other action. The syntax for the OnAction property is:
Ctrl.OnAction = “=fnSomeFunctionName()”
The function defined by the text within the quotes in the example above must be the name of a PUBLIC function, must be preceded by the equal sign, and must contain the opening and closing parenthesis at the end of the string. If the function is defined as having mandatory parameters, then you must pass a value to the function within the parenthesis, just like you would if you were calling that function from some other code module in your form.
Ctrl.OnAction = “=fnSomeFunctionName(1)”
'Or
Ctrl.OnAction = “=fnSomeFunctionName(‘SomeTextValue’)”
An alternative to this method is to define your functions without the arguments and use the [Parameter] property of the control to assign a value to the control. If you use this technique, then within the function defined by your OnAction property, you can determine the value of the parameter of the control that was clicked with the following line of code.
CommandBars.actioncontrol.parameter
There are a number of other properties associated with each control which you can browse using IntelliSense. Generally, the Tag (used like a controls Tag property) and BeginGroup (draws a line in the menu above the current control) are the most commonly used of the other control properties.
Public Sub FormOptions(Optional Reset As Boolean)
'the constants used in this code are declared in the example database
Dim cbr As CommandBar 'Object '
Dim cbrButton As CommandBarButton 'Object '
On Error GoTo FormOptionsError
If CmdBarExists("FormOptions") Then 'see sample database
If Reset = False Then Exit Sub
DeleteCmdBar "FormOptions" 'see sample database
End If
'Instantiate the commandbar
Set cbr = CommandBars.Add("FormOptions", BarPopup, , True)
'Create the two buttons
With cbr
Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
With cbrButton
.Caption = "Display All"
.Parameter = "All"
.State = -1
.OnAction = "=fnFormOptionsDisplayRecords()"
End With
Set cbrButton = cbr.Controls.Add(ControlButton, , , , True)
With cbrButton
.Caption = "Display Active"
.Parameter = "Active"
.OnAction = "=fnFormOptionsDisplayRecords()"
End With
End With
Exit Sub
FormOptionsError:
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly, "FormOptions Menu error"
Debug.Print "FormOptions error", Err.Number, Err.Description
End Sub
This code includes calls to several other procedures (CmdBarExists, DeleteCmdBar) which are provided in the example database. You should note that the State of the "Display All" button is initially set to -1, to place a check-mark next to it. Also note that both buttons call the same function in their OnAction events, and have different parameter values. Use of that parameter value can be seen in the following OnAction function:
Public Function fnFormOptionsDisplayRecords()
Dim strAction As String
strAction = CommandBars.ActionControl.Parameter
'Change the state of the two buttons
CommandBars("FormOptions").Controls("Display All").State = (strAction = "All")
CommandBars("FormOptions").Controls("Display Active").State = (strAction = "Active")
'Filter or clear the filter, depending on which button was selected
If strAction = "All" Then
Form_frm_CommandBars.sub_Presidents.Form.Filter = ""
Form_frm_CommandBars.sub_Presidents.Form.FilterOn = False
Else
Form_frm_CommandBars.sub_Presidents.Form.Filter = "[Active] = -1"
Form_frm_CommandBars.sub_Presidents.Form.FilterOn = True
End If
End Function
This code determines which of the two buttons was clicked and assigns that string value to the variable strAction. It then toggles the two buttons on/off (up/down) based on the value of strAction. And finally, the code in the If / End If segment either sets the subforms filter or clears it.
Set cbrcombo = cbr.Controls.Add(ControlComboBox, , , , True)
With cbrcombo
.Caption = "Display records:"
.BeginGroup = True
.Width = 150
.DropDownWidth = 100
.AddItem "All"
.AddItem "Active"
.OnAction = "=fnFormOptionsDropdown()"
.ListIndex = 1
End With
The differences between this and the button code are the addition of the DropDownWidth property to define the width of the dropdown, the addition of the .AddItem methods to add items to the combo boxes list, and the addition of the ListIndex property to set the default "value" of the combo box. When determining the "Value" of the combobox in function fnFormOptionsDropDown(), you must use the controls ListIndex property. Although this is not as simplistic as the using the "Parameter" property of the button control, it provides the ability to add add multiple values in a small space. If you have a large number of items to add to the combo box, you can create a recordset and loop through that recordset to add multiple values to the combo. If you really wanted to, you could create a cascading combo box functionality by using the controls .Clear method and then rebuilding a comboboxes Item list in the function called by the OnAction event of another combo box.
Set cbrEdit = cbr.Controls.Add(ControlEdit, , , , True)
With cbrEdit
.Caption = "Year:"
.OnAction = "=fnFormOptionsYear()"
End With
and to display the entered value in a message box after it is entered. Note that the CommandBar textbox does not have a "Value" property. You must refer to the "Text" property of the control to determine what was entered into that control:
MsgBox CommandBars("FormOPtions").Controls("Year:").Text
Working with Popups
If Button = acRightButton Then CommandBars("FormMenu").ShowPopup
This process a little more effort, since you must add a line of code to the MouseUp event of each of the controls where you want to implement your shortcuts, but it is worth it to get rid of the form shortcut menu and still have access to your custom Commandbars.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (37)
Commented:
Commented:
Author
Commented:Are you trying to create a shortcut menu for Word?
Since you are talking about Word 2010, I'm inclined to think you are going to have to dive into the Ribbon, but I have no idea how to do that in Word (or Access) for that matter.
Dale
Commented:
Commented:
View More