By Todd E. Parker
Give your Command Buttons that interactive look and feel. Why use those boring Command Buttons when you can have them change color when the mouse moves over them? Here is one way you can create that functionality. (This Access tip assumes you have an understanding of Microsoft’s Visual Basic for Applications (VBA) in Access database applications.)
The function you will create is versatile, as it can work with any form throughout your application. The code executes at super speeds due to its simplicity. Your users will not see any lag in the application while the mouse-over routine is executed for each button.
Note that there are ActiveX controls that you can add to your application through the use of references that will accomplish the same task. This tip provides a means to avoid using those ActiveX controls. The benefits of writing these functions yourself are:
- · With less references loaded to outside controls, you are freeing valuable resources for your application. The reason for this is because most ActiveX controls are packed with tons of other features you may not want or need.
- · You will save yourself the registration fees of these ActiveX controls.
- · You will learn to be self-sufficient.
- · Finally, you will be able to better control how your applications work, rather than relying on the methods and available features of the ActiveX control.
There are several steps involved in the creation of your own streamlined method for performing this trick; I will explain each step for one possible method.
First, you must modify each of the buttons’ Tag property, and then add “MouseMove” event procedures for each button. Finally, you will add a function that performs the color change.
Step 1. Modify your Command Button’s Tag property
For each command button to which you want to apply this behavior, you must modify the Tag property by giving it a name and a unique number. For instance, we will pretend that we have three buttons. The Tag property for each would be:
You must also modify the Tag of the Header and Detail sections of your form. Each time the mouse moves off of the buttons and onto the form itself, you must run the color function again in order to set all of the buttons back to black. Otherwise, the last button that had its color changed would stay that way. The only difference is that the tags are NOT unique:
Step 2. Insert a MouseMove event for all of the buttons and the header/detail sections
Access uses the MouseMove event property to run code each time the mouse moves over the Control. Click here to see the code to insert.
Your options for button colors
How do you define a specific color for your buttons? Use these numbers in the following code to specify the color for the button to change to when the user moves the mouse over it.
- · MyColor = 0 <– Makes All Buttons return to Black
- · MyColor = 1 <– Makes the Button turn Blue
- · MyColor = 2 <– Makes the Button turn Red
- · MyColor = 3 <– Makes the Button turn Green
- · MyColor = 4 <– Makes the Button turn Yellow
- · MyColor = 5 <– Makes the Button turn White
Step 3. Dealing with mouse movement
The next step is to insert a new Module called “MouseMoveFunctions.” Inside this module, insert the code (click here to the view the code).
When the mouse moves over the Command Buttons, the MouseMove event is fired. In this event, the Tag of the Command Button and a color number are sent to the SetMouseOverColor function. In this function, controls that are not black are set back to black, and the current Button with the mouse over it changes to the specified color.
From this point forward, you can set up each form in your Application to use the SetMouseOverColor function.
Todd E. Parker is a developer for the Science Applications International Corporation in New Orleans.