Microsoft KB Archive/113304

= ACC2: How to Create a Custom Toolbar Using a Form =

Article ID: 113304

Article Last Modified on 7/8/2002

-

APPLIES TO


 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q113304



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



SUMMARY
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:


 * Labels, combo boxes and list boxes can be placed on the toolbar.
 * Buttons with custom bitmaps can be placed 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 from being used on the toolbar.



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.

 Open the sample database NWIND.MDB and create a new, blank form.  Set the following form-level properties:

      Caption: Toolbar ShortcutMenu: No      ScrollBars: Neither RecordSelectors: No      NavigationButtons: No       PopUp: Yes BorderStyle: Dialog MinButton: No      MaxButton: No                         Activate the toolbox by choosing Toolbox from the View menu. Make sure the Wizard button is chosen (the button should appear sunken). Choose the command button tool, and then click anywhere in the form. In the Categories box, select Record Navigation. In the When Button Is Pressed box, select Find Record. Choose Finish. Drag the new button to the upper left corner of the detail section.</li>  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.

<pre class="fixed_text">      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 </li> 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.</li> From the View menu, choose Code.</li>  Create the following Access Basic function in the toolbar form module:

<pre class="fixed_text">      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. </li>  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.

<pre class="fixed_text">      If ActivateToolbarForm = False Then Exit Sub

For example, the code for the Search button might look like the following:

<pre class="fixed_text">      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. </li> Save the form with the name Toolbar, and then close the form.</li>  Create a new Access Basic module. Add the following code:

<pre class="fixed_text">      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. </li>  For every form that you intend to use the custom toolbar with, add the following line of code to the form's OnActivate property:

<pre class="fixed_text">      SetToolbarForm Me

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

<ol style="list-style-type: lower-alpha;"> Open the Employees form in Design view.</li> From the View menu, choose Code.</li> Select Form in the first combo box on the code toolbar.</li> Select Activate in the second combo box on the code toolbar.</li>  Add the above line of code so the subroutine appears as follows:

<pre class="fixed_text">         Sub Form_Activate SetToolbarForm Me         End Sub

</li> Repeat steps a-e for the Customers form.</li></ol>

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. </li></ol>

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
<ul>  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:

<pre class="fixed_text">     Sub Form_Load DoCmd OpenForm "Toolbar" End Sub

Sub Form_Unload (Cancel As Integer) DoCmd Close A_FORM, "Toolbar" End Sub

</li> <li>You can set the BorderStyle property of the toolbar form to None, so that no border and no caption bar appears. This will prevent users from being able to move the toolbar around.</li> <li> You may want to automatically position the toolbar form to a specific location on the screen. To do this, use a MoveSize macro action in the toolbar form's OnLoad property. The following sample code will position the toolbar form to the upper left corner of the screen:

<pre class="fixed_text">     DoCmd MoveSize 0, 0

</li></ul>

<div class="references_section">