<

How to code "Mouse Over" effects for MS Access

Published on
57,023 Points
46,923 Views
6 Endorsements
Last Modified:
Awarded
Community Pick
This tutorial is written as a how-to for a novice to master at creating, manipulating and coding MS Access databases. This tutorial assumes that you have a general understanding of how the basic functions of Access are used, as well as how to view the underlying code within the database. I have included a sample database to allow you to see the code in action, and how it can be used, as well as some other helpful resources that come in handy when using this method. The code is also included below for easy reference.

Please note that as with anything there are many ways to do things and everyone attacks a problem from their own angle or perspective.

That being said let's get to it. I searched for a Mouse Over like effect for a project that I worked on back in 2001, it was then that I realized that Microsoft had left a lot of functionality out of the Access application. So I did like most of us here as member of EE: I searched for a previously found solution on EE. I found some things that kind of worked or did what I needed but not exactly. So I played with it for a while, and created a few test applications to try different things as well as to work out the bugs. That has provided me with a set of standard tools that I use now in application after application.

 

My guide to "Mouse Over Effects" within Microsoft Access


The type of object you are applying the Mouse Over Effect to, will determine which way is best. For instance, you apply the effects differently for a text box, or label than you would for a shape, such as a square or a border. This tutorial covers the simplest implementation of my technique, but it can be changed to obtain more complex visual effects.
 

1. Mouse Over for text box and label

In this section I will cover Mouse Over Effect for a text box, using a change in the font size, the font color, and font face.
 
Create a new form save and rename it "frmYOUR FORM NAME".

Open the form in design view, and add a text box to the form.

Select the text box, and view the properties pane, select the other tab and name the text box "txtbxYOUR TEXTBOX NAME" repeat for the attached label "lblYOUR LABEL NAME".
Screenshot1
Now one thing to keep in mind is the fact that the font will be increasing inside the label or text box area, so you need to give it enough room to do so. Increase the size of the label or text box in both height and length:
Screenshot2
Click on your text box to select it and view the properties pane.

Select the Event tab, then in the On Mouse Move event and click on the button with the three little dots on it. This will open a window allowing you to choose how you want to control this event. Select Code Builder and click OK.
Screenshot3
This will open the MS Visual Basic editor for the current database. You will use this interface to make all of the code changes and additions to your database project.

In the editor we are going to call the control for the text box and label that we just created, using "Me." and the control name.

Use this code to increase the font size, font color and font face of your control.

Private Sub txtbxMouseOverTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Create a Mouse Over Effect for the text box by increasing font size, changing font color, and font face

Me.txtbxMouseOverTest.FontSize = 14
Me.txtbxMouseOverTest.ForeColor = vbRed
Me.txtbxMouseOverTest.FontBold = True

Open in new window

 

Now we must set the default state for the contorls. This means anytime the mouse is moved off of the text box or the label, and over the Detail section of the form the text box and label will return to their default state.

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Set the default state for the controls that will have an onMouseOver effect applied

Me.txtbxMouseOverTest.FontSize = 12
Me.txtbxMouseOverTest.ForeColor = vbBlack
Me.txtbxMouseOverTest.FontBold = False

'If you want the label to change as well add its default state here

Me.lblMouseOverTest.FontSize = 12
Me.lblMouseOverTest.ForeColor = vbBlack
Me.lblMouseOverTest.FontBold = False
End Sub

Open in new window

 

Now that the default state is set, we can test our Mouse Over Effects.
 

Open your form in form view, and hover your mouse over the text box.  You should see the text font size increase, as well as the color change to red.  Let's move on to mouse over effects on shapes.
Remember: The control must be oversized enough to accommodate your choice of font size so you may have to tweak this if you are going to increase the font size drastically.

End Tutorial for Mouse Over Effects on text box and label.

 

2. Mouse Over for a shape

This Section will cover how to apply some of the same thinking to a shape on your form.
 
On the form that you have already created, add a rectangle using the rectangle tool.

Name it "bxYOUR REC NAME", and set its format properties so that it has a 3 point solid border, with a background color that matches the form detail.
Important: The shape must have a normal background. If you set it to transparent the Mouse Move event will only be effective when your mouse is over the line that creates the shapes border. By filling in the shape the effect will work anytime you mouse is over any part of the shape.
 Screenshot4
Again we are going to have to set the shapes default settings in the detail on mouse move event. So lets go ahead and set the default state now.

Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Set default state for rectangle

Me.bxMouseOverTest.BorderColor = vbBlack
Me.bxMouseOverTest.BorderWidth = 3

End Sub

Open in new window

 

Now we can add the desired Mouse Over Effect. For my example I am just going to have the rectangle border color change. It is simple but effective for a shape.

Private Sub bxMouseOverTest_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Create a Mouse Over Effect for the rectangle, changing its border color to red

Me.bxMouseOverTest.BorderColor = vbRed

End Sub

Open in new window

 

Now we can test it and make sure that it works. Open your form in form view and move your mouse over any part of the shape to ensure that it works.  If so, you have just completed your first Access Mouse Over effects programing. Well done!!
End Tutorial for Mouse Over effects on shapes.


NOTES:

In some cases the Mouse Over effect my appear to flicker or flutter, if this occurs you more than likely have some sort of a form repaint or requery action occurring. If that is the case you may have to throw some IF statement onto the form detail section to ensure that your text box or label are in the desired default state prior to the mouse moving over them.

Example:
 
'This will give the label a on mouse over effect - return to original font size
If Me.lblYOUR LABEL.FontSize = 10 Then Me.lblYOUR LABEL.FontSize = 9
If Me.lblYOUR LABEL1.FontSize = 10 Then Me.lblYOUR LABEL1.FontSize = 9
If Me.lblYOUR LABEL2.FontSize = 10 Then Me.lblYOUR LABEL2.FontSize = 9

Open in new window


Also the example I provided only covers changing control colors using built in vb colors, which limits your options. If you refer to the Excel document that is attached you will find the conversion table for over 300 colors. Converts from HEX to RGB and Access. The code has to change slightly when using a different method of choosing colors. For instance, if you were to use an Access color code you would code it as follows.
 
Me.lblYOUR LABEL NAME= 221695

Open in new window

Which would be an orange color.


This concludes my tutorial on how to force MS Access to emulate a Mouse Over Effect programmatically. I hope that you found it somewhat helpful. If you have any questions or comment please feel free to shoot them to me here on EE.

Mouse-over-effects.accdb
Programing-Color-Referance.xlsx
6
Comment
2 Comments
 
LVL 58

Expert Comment

by:harfang
Good article!

I understand that this is a tutorial to explain the basic concept, but I tend to take the final comments seriously: this technique may indeed cause “flicker or flutter”. The technical reason is that a formatting change forces a form redraw, which in turn forces an empty mouse move event (even if the mouse didn't move). This causes a continuous cycle of redraws.

I always try to perform formatting changes only when needed (e.g. not set the colour to black if it's already black), exactly like in the suggested code at the end. But perhaps this is because I have worked on very slow computers in the past...

Voted yes!
(°v°)
0
 

Expert Comment

by:Alan Bell
Thanks - I found this quick tip extremely useful - I like to use mouse over to highlight to the User exactly what cell they are looking at.......and the flickering was really annoying me......Simpler is better...!!

Thanks
Alan Bell 09/03/2017
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Wrapper-1-Query. Use an Excel function to calculate a column for an Access query. Part 1. Shows a query in Access that has a calculated column with the results of an Excel worksheet function. See how to call a wrapper function from a query, and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month