<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

How to code "Mouse Over" effects for MS Access

Published on
60,748 Points
50,648 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 you have a general understanding of how to use the basic functions within MS Access, as well as how to view the underlying VBA code. I have included a sample database to allow you to see the code in action, and how it is utilized, 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 is more than one way to do things and everyone attacks a problem from his or her angle or perspective.

I searched for a Mouse Over like effect for a project I worked on in 2001, and it was then that I realized that Microsoft failed to include some common functionality in the Access application. I found some solutions that partially did what I needed, but none were exactly what I was looking for. So I went to work and created a few test applications trying different things and identifying the bugs. The outcome provided a set of standard tools that I now use in many of my applications.

This is my guide to "Mouse Over Effects" within Microsoft Access.

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

 

1. Mouse Over for text box and label

This section will cover applying a Mouse Over Effect to 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

 Keep in mind the font will be increasing inside the label or textbox area, therefore the label or textbox must be large enough to accommodate the increase in size. Increase the size of the label or textbox in both height and length:

Screenshot2

 Click on your text box to select it and view its properties.


 Select the Event tab, then in the On Mouse Move event and click on the button with three little dots. This will open a window allowing you to choose how you want to control this event. Select Code Builder and click OK.

Screenshot3

 Code Builder 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 call the control for the text box and label you just created, using "Me." and the control name.


 The code below will 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

 


 Now set the default state for the controls. This means anytime the mouse is moved off of the text box or the label, and over the Detail section of the form the textbox 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

 


 Now that the default state is set, you can test the Mouse Over Effect.

 


 Open the 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 it may be necessary 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 covers how to apply some of the same thinking to a shape on your form.

 

 On the form you 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 border of the shape. By filling in the shape the effect will work anytime you mouse is over any part of the shape.

 Screenshot4

 Set the shapes default settings in the detail on mouse move event. Set the default state using the code below.


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

 


 Now 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

 


 Now test it to ensure it works properly. Open your form in form view and move your mouse over any part of the shape, you should see the event trigger and the border color change to red.  If so, you have successfully completed your first Access Mouse Over effects programming. Well done!!

End Tutorial for Mouse Over effects on shapes.



NOTES:


In some cases, the Mouse Over effect may 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 is in the desired default state prior to the mouse moving over them see code example below.


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


The example provided only covers changing control colors using built-in vb colors, limiting your color options. If you refer to the attached Excel document, you will find the conversion table for over 300 colors. It provides information to convert from HEX to RGB and MS Access. The code has to change slightly when using a different method of choosing colors. For instance, if you were to use an MS Access color code the syntax would be as follows.

 

Me.lblYOUR LABEL NAME= 221695

This example is an orange color.



This concludes my tutorial on how to force MS Access to emulate a Mouse Over Effect programmatically. I hope you found it helpful. If you have any questions or comments please feel free to me 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

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Join & Write a Comment

Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month