Microsoft KB Archive/142187

From BetaArchive Wiki

Article ID: 142187

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft Access 95 Standard Edition



This article was previously published under Q142187

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:

  • You can place labels, combo boxes, and list boxes on the toolbar.
  • You can place buttons with custom bitmaps on the toolbar.
  • You can prevent users from closing or moving the toolbar.
  • You can take advantage of form modules to store the code being used by the controls on the toolbar with the toolbar itself.
  • You can disable the shortcut menus on the toolbar.

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:

150895 ACC95: Microsoft Access Sample Forms Available in Download Center

175066 ACC97: Microsoft Access 97 Sample Forms Available in Download Center


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:

    1. Open the Employees form in Design view.
    2. On the View menu, click Code.
    3. Click Form in the Object box at the top of the Module window.
    4. Click Activate in the Procedure box at the top of the Module Window.
    5. Add the "SetToolbarForm Me" line of code so that the subroutine reads 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 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

  • You may want to automate the loading and closing of a custom toolbar form from another form's Load and UnLoad events. For example, to have the custom toolbar open only with the Customers form, add the following code to the Customers form's OnLoad and OnUnload properties:

          Sub Form_Load ()
             DoCmd.OpenForm "Toolbar"
          End Sub
    
          Sub Form_Unload (Cancel As Integer)
             DoCmd.Close ACFORM,  "Toolbar"
          End Sub
                            
  • You can set the BorderStyle property of the Toolbar form to None so that no border and no caption bar appear. This will prevent users from being able to move the toolbar around.
  • You may want to automatically position the Toolbar form at a specific location on the screen. To do so, use a MoveSize macro action in the Toolbar form's OnLoad property. The following sample code positions the Toolbar form at the upper-left corner of the screen:

          DoCmd.MoveSize 0, 0


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:

113304 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: kbhowto kbprogramming kbusage KB142187