Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Mar 2006
    Thanked 0 Times in 0 Posts

    option button in toolbar (Access 2000)

    Can somebody help me build an option button in a toolbar ? I mean a button with dropped down 2 possibilities needed to build a code.I can build a single button with the code shown below.i will be grateful if someone could help me built an option button with the same code.

    Sub ToolbarCustomers()

    Dim cbr As Object
    Dim cbb As Object
    ' msoBarTop = 1
    Set cbr = CommandBars.Add(Name:="MyToolbarCustomers", Position:=1, Temporary:=True)

    1. msoControlButton = 1 First buton MyBack2
    Set cbb = cbr.Controls.Add(Type:=1, Temporary:=True)
    With cbb
    '.Caption = "My Button"
    ' msoButtonCaption = 2
    ' .Style = 2
    .FaceId = 41
    .Width = 60
    .FontWeight = 800

    .OnAction = "MyBack2"
    End With

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Arlington, Virginia, USA
    Thanked 3 Times in 3 Posts

    Re: option button in toolbar (Access 2000)

    There's no such thing as an "Option Button" or Option Group for toolbar, the Office CommandBar object model does not provide this functionality. Your choices for toolbar controls are limited to command buttons, menus, edit controls, and combo boxes. You can simulate the functionality of an Option Group by setting the CommandBarButton State property to "Up" (not selected) or "Down" (selected) to indicate which "option" is currently selected.

    Attached file (Access 2K format) demonstrates how to do this using the CommandBarButton OnAction property. Custom toolbar "Toolbar 1" should be displayed when open db. When you click one of the card suit buttons, the button will be "selected". The buttons under "Menu 1" are grouped into "Option Groups" - click buttons to see how works. The text-only menu items will display a checkmark to indicate the "selected" option. In actual use you'd replace the dummy msgbox with actual code to run when button is selected. The code is generic so that you don't have to specify which group the button is in - just be sure that each button in "option group" has the same Tag property, and that the Tag is unique for each group.

    <code>Public Function ButtonOnActionFunction()</code>
    <code> SetToolbarButtonState</code>
    <code> MsgBox "Toolbar Button " & Application.CommandBars.ActionControl.Caption & " OnAction function.", _</code>
    <code> vbInformation, "BUTTON ON ACTION FUNCTION"</code>
    <code>End Function</code>

    <code>Private Function SetToolbarButtonState()</code>
    <code> On Error GoTo Err_Handler</code>

    <code> Dim ctl As Office.CommandBarControl</code>
    <code> Dim ctlActive As Office.CommandBarControl</code>
    <code> Dim strMsg As String</code>
    <code> Dim strCaption As String</code>

    <code> Set ctlActive = Application.CommandBars.ActionControl</code>
    <code> strCaption = ctlActive.Caption</code>

    <code> ' Loop thru controls in parent object:</code>
    <code> For Each ctl In ctlActive.Parent.Controls</code>
    <code> ' test Tag property to match that of active button control</code>
    <code> If ctl.Tag = ctlActive.Tag Then</code>
    <code> If ctl.Caption = strCaption Then</code>
    <code> ' "Select" item:</code>
    <code> ctl.State = msoButtonDown</code>
    <code> Else</code>
    <code> ' "De-Select" item:</code>
    <code> ctl.State = msoButtonUp</code>
    <code> End If</code>
    <code> Else</code>
    <code> ' do nothing - not in same "Option group"</code>
    <code> End If</code>
    <code> Next ctl</code>

    <code> Set ctl = Nothing</code>
    <code> Set ctlActive = Nothing</code>
    <code> Exit Function</code>
    <code> Select Case Err.Number</code>
    <code> Case 0</code>
    <code> Resume Next</code>
    <code> Case Else</code>
    <code> strMsg = "Error No " & Err.Number & ": " & Err.Description</code>
    <code> MsgBox strMsg, vbExclamation, "SetToolbarButtonState - Unexpected Error"</code>
    <code> Resume Exit_Proc</code>
    <code> End Select</code>
    <code>End Function</code>

    Note - can also define event procedures for command bar controls but in this case was simpler to use OnAction property to run code.


  3. #3
    2 Star Lounger
    Join Date
    Mar 2006
    Thanked 0 Times in 0 Posts

    Re: option button in toolbar (Access 2000)

    Thank you Mark !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts