ACC95: How to Create a Custom Toolbar Using a Form

ID: Q142187

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser 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:

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access for Windows 95" manual.

NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q150895
   TITLE     : ACC95: Microsoft Access Sample Forms Available on MSL

   ARTICLE-ID: Q175066
   TITLE     : ACC97: Microsoft Access 97 Sample Forms Available on MSL

MORE INFORMATION

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

1. Open the sample database Northwind.mdb and create a new, blank form.

2. Set the following form properties:

      Caption: Toolbar
      ShortcutMenu: No
      ScrollBars: Neither
      RecordSelectors: No
      NavigationButtons: No
      PopUp: Yes
      BorderStyle: Dialog
      Min Max Buttons: None

3. If it is not displayed, display the toolbox by clicking Toolbox on the
   View menu. Make sure the Control Wizard button is selected.

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

5. In the Command Button Wizard, under Categories, click Record

   Navigation. Under Actions, click Find Record. Click 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 Action
   boxes in the Command Button Wizard. Move each of the new buttons
   directly to the right of the button before it.

       Categories             Actions
       --------------------------------------------
       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. On the View menu, click Code.

10. Create the following Visual 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 that 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, move the focus to a button by single clicking it. Using the right
    mouse button, click in the OnClick property field of the button, and
    then click the Build button.

       If ActivateToolbarForm() = False Then Exit Sub

    The code for the Search button might look as follows:

       Sub Command0_Click()
          If ActivateToolbarForm() = False Then Exit Sub

          On Error GoTo Err_Command0_Click

          Screen.PreviousControl.SetFocus
          DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

       Exit_Command0_Click:
          Exit Sub

       Err_Command0_Click:
          MsgBox Err.Description
          Resume Exit_Command0_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 Visual 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 of Northwind.mdb. You can copy this
    function into your database.

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 Employees and Customers forms. To do so, follow these steps:

     a. Open the Employees form in Design view.

     b. On the View menu, click Code.

     c. Click Form in the Object box at the top of the Module window.

     d. Click Activate in the Procedure box at the top of the Module
        Window.

     e. Add the "SetToolbarForm Me" line of code so that the subroutine
        reads as follows:

          Sub Form_Activate ()
             SetToolbarForm Me
          End Sub

     f. 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 Employees and Customers 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 2.0, please see the following article here in the Microsoft Knowledge Base:

   ARTICLE-ID Q113304
   TITLE:     ACC2: How to Create a Custom Toolbar Using a Form

For more information about toolbars, search on the phrase "Customizing Toolbars," and then view "Modify Toolbars or Toolbar buttons" using the Answer Wizard from the Microsoft Access for Windows 95 Help menu.
Keywords          : kbusage FmsHowto 
Version           : 7.0
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: October 25, 1997