Visit our new web pagesWe have launched a new extended set of web pages at www.alvechurchdata.co.uk. These old pages will stay on the server because people still have links to them but they will not be updated. The new version of this page is at www.alvechurchdata.co.uk/hints-and-tips/accaddtoolbar.html. |
||
Home About us Development Training Support |
Custom Toolbars in AccessYour Access application will look more impressive and be easier for the users if you add some custom toolbars. One way of doing this is to right-click on one of the existing toolbars and select but it's safer to write some VBA code and let the application create the toolbars for you. Access gives you complete control of these custom toolbars. You can create and destroy them and determine whether or not they are visible to a particular user.
This example shows a simple Customer form with its own toolbar. This toolbar has the correct Windows icons and a familiar tooltip and will give the user confidence in the quality of the database you've developed. References for a custom toolbarThe code that creates this toolbar is in the Load event of the Customer form. Before writing any code you must ensure that you have a reference to the right library. The toolbar and its buttons are instances of a and objects and these are not part of the core of Microsoft Access. You must tell Access where it can find them.Open a code editing window and select from the menu.
The contents of this list will vary with the software already installed on your PC. You need to scroll down to the block of Microsoft software and tick the entry for the Microsoft Office Object Library. This example shows an entry for Access 97 in Office 8.0. Note that this reference will have to be updated if you try to run this code on a machine running a different version of Microsoft Office. If you copy the database to a machine running Office XP then you will be able to open the database with Access XP but you will get an error message saying that it cannot find the Office 8.0 library. The solution is to open the code window on the new PC, clear the Office 8.0 tickbox and tick the box for the version of Office running on that PC. Create a custom toolbarThe first thing to do is to create the toolbar as a CommandBar object:
'-- Create a command bar with three buttons
Set myBar = CommandBars.Add(Name:="Customer", _ Position:=msoBarTop, _ Temporary:=True) With myBar .Controls.Add Type:=msoControlButton, Id:=539 .Controls.Add Type:=msoControlButton, Id:=4 .Controls.Add Type:=msoControlButton, Id:=644 .Visible = True End With The property is set True to define this as a temporary toolbar. It will disappear when the database is closed. This property defaults to False and any toolbar that you create will persist and reappear the next time that you open the database. If you do use a permanent toolbar then you must add error-handling code to ensure that you do not try to create it a second time.A command bar can hold several different types of control and the parameter here declares that we are adding three command buttons. The parameter defines the icon that will appear on these buttons and the action that the button will perform if we do not override it later. Set an Id of 1 if you want a button with no icon.The codes are not well-documented in the Access Help system. Follow this link for a list of the most useful ID codes . Define the buttonsEach button on the toolbar is a member of the Controls collection of the CommandBar. This fragment of code defines the third button on the toolbar:
'-- Define the Delete button
With myBar.Controls(3) .Style = msoButtonIconAndCaption .Caption = "Delete Customer" .OnAction = "=DeleteCustomer()" .TooltipText = "Delete all records of this customer" End With The property is set to show an icon and some text. You can also choose or to show an icon or some text alone.The property defines the action that must be performed when this button is clicked. In this example it calls a method of the form.Show and hide a toolbarUse the and properties of the CommandBar object to control whether or not the user can see and operate the buttons on the toolbar.Removing a toolbarThis code in the form's event removes the toolbar from the collection of CommandBars. There is always the possibility that the user might have closed the toolbar manually so we have to work around this possible error.
'-- Simple-minded error handling in case the user has
'-- already deleted this toolbar. On Error Resume Next CommandBars("Customer").Delete |
Hints & tips
The textbox class in Visual FoxPro 9 has a new Autocomplete
property which shows the user the previous values that have
been entered in that textbox.
More...
|