ACC2: How to Create a Custom Toolbar Using a Form

ID: Q113304


The information in this article applies to:


SUMMARY

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

This article describes how to create a custom, floating toolbar using a Microsoft Access form. Using a form for a custom toolbar provides the following advantages:


MORE INFORMATION

The example below demonstrates how to create a custom, floating toolbar that has buttons to find, save, delete, and undo a record, as well as buttons to navigate among the records, including buttons to go to the first record, go to the previous record, go to the next record, go to the last record, and add a new record.

  1. Open the sample database NWIND.MDB and create a new, blank form.


  2. Set the following form-level properties:
    
           Caption: Toolbar
           ShortcutMenu: No
           ScrollBars: Neither
           RecordSelectors: No
           NavigationButtons: No
           PopUp: Yes
           BorderStyle: Dialog
           MinButton: No
           MaxButton: No 


  3. Activate the toolbox by choosing Toolbox from the View menu. Make sure the Wizard button is chosen (the button should appear sunken).


  4. Choose the command button tool, and then click anywhere in the form.


  5. In the Categories box, select Record Navigation. In the When Button Is Pressed box, select Find Record. Choose Finish.


  6. Drag the new button to the upper left corner of the detail section.


  7. Repeat steps 4-6 to create the other buttons for the toolbar. Use the following selection combinations from the Categories and the When Button Is Pressed boxes. Move each of the new buttons directly to the right of the button before it.
    
           Categories             When button Is Pressed
           ---------------------------------------------
           Record Operations      Save Record
           Record Operations      Delete Record
           Record Operations      Undo Record
           Record Navigation      Go to First Record
           Record Navigation      Go to Previous Record
           Record Navigation      Go to Next Record
           Record Navigation      Go to Last Record
           Record Operations      Add New Record 


  8. Drag the bottom of the detail section up so that it is flush with the bottom of the buttons. Drag the right side of the detail section so that it is flush with the right side of the last button.


  9. From the View menu, choose Code.


  10. Create the following Access Basic function in the toolbar form module:
    
           Option Explicit
    
           Function ActivateToolbarForm ()
              On Error Resume Next
              Forms(Me.Tag).SetFocus
              If Err Then
                 ActivateToolbarForm = False
              Else
                 ActivateToolbarForm = True
              End If
           End Function 

    This function will be used to reactivate the form the toolbar is floating on, so that the chosen operation is performed on that form rather than against the toolbar form itself.


  11. For each button on the toolbar form, insert the following line of code at the very top of the button's OnClick event procedure. To insert the code, select a button, click the secondary mouse button in the OnClick property field, and then choose Build.
    
           If ActivateToolbarForm() = False Then Exit Sub 

    For example, the code for the Search button might look like the following:
    
           Sub Button0_Click ()
           If ActivateToolbarForm() = False Then Exit Sub
           On Error GoTo Err_Button0_Click
    
              DoCmd DoMenuItem A_FORMBAR, A_EDITMENU, 10, , A_MENU_VER20
    
           Exit_Button0_Click:
              Exit Sub
    
           Err_Button0_Click:
              MsgBox Error$
              Resume Exit_Button0_Click
           End Sub 

    This code ensures that the form the toolbar is floating on is selected for the chosen operation.


  12. Save the form with the name Toolbar, and then close the form.


  13. Create a new Access Basic module. Add the following code:
    
           Option Explicit
    
           Sub SetToolbarForm (F As Form)
              If IsLoaded("Toolbar") Then Forms![Toolbar].Tag = F.Name
           End Sub 

    The SetToolbarForm subroutine uses the IsLoaded() function that is in the Utility Functions module in NWIND.MDB. You should copy this function for use in other databases.


  14. For every form that you intend to use the custom toolbar with, add the following line of code to the form's OnActivate property:
    
           SetToolbarForm Me 

    For this example, add the line of code above to the OnActivate property of the Customers and Employees forms:

    1. Open the Employees form in Design view.


    2. From the View menu, choose Code.


    3. Select Form in the first combo box on the code toolbar.


    4. Select Activate in the second combo box on the code toolbar.


    5. Add the above line of code so the subroutine appears as follows:
      
                Sub Form_Activate ()
                   SetToolbarForm Me
                End Sub 



    6. Repeat steps a-e for the Customers form.



    This line of code instructs the custom toolbar to store the name of the form to be used when a toolbar button is chosen. This ensures that the toolbar actions are performed against the active form.


Using the Custom Toolbar

Open the Customers and Employees forms in Form view, and then open the Toolbar form. Switch back and forth between the Employees form and the Customers form, using the navigation buttons on the custom toolbar to navigate among the form records.

Suggested Enhancements to the Custom Toolbar



REFERENCES

For information about how to create a custom toolbar using a form in Microsoft Access for Windows 95 version 7.0, please see the following article here in the Microsoft Knowledge Base:

Q142187 ACC95: How to Create a Custom Toolbar Using a Form

Additional query words: tool bar


Keywords          : kbusage FmsOthr 
Version           : 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 3, 1999