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:
- 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:
Q150895
ACC95: Microsoft Access Sample Forms Available in Download Center
Q175066
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.
- Open the sample database Northwind.mdb and create a new, blank form.
- Set the following form properties:
Caption: Toolbar
ShortcutMenu: No
ScrollBars: Neither
RecordSelectors: No
NavigationButtons: No
PopUp: Yes
BorderStyle: Dialog
Min Max Buttons: None
- If it is not displayed, display the toolbox by clicking Toolbox on the
View menu. Make sure the Control Wizard button is selected.
- Click the command button tool, and then click anywhere in the form.
- In the Command Button Wizard, under Categories, click Record
Navigation. Under Actions, click Find Record. Click Finish.
- Drag the new button to the upper-left corner of the detail section.
- 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
- 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.
- On the View menu, click Code.
- 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.
- 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.
- Save the form with the name Toolbar, and then close the form.
- 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.
- 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:
- Open the Employees form in Design view.
- On the View menu, click Code.
- Click Form in the Object box at the top of the Module window.
- Click Activate in the Procedure box at the top of the Module
Window.
- Add the "SetToolbarForm Me" line of code so that the subroutine
reads as follows:
Sub Form_Activate ()
SetToolbarForm Me
End Sub
- 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:
Q113304
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.
Additional query words:
Keywords : kbusage FmsHowto
Version : WINDOWS:7.0
Platform : WINDOWS
Issue type : kbhowto
|