Here’s another database I did to help a fellow Access developer on LinkedIn. The question asked was “I would like to find an inexpensive needle gauge (like a speedometer) that can be used with Access 2007 to produce a dashboard application”.
Well, I decided to have a play around with a standard Access Line control to see if it could be used as a “speedometer needle”. After checking out all the properties and methods available for the Line class, I realised that with a bit of geometry thrown in this could be done.
No, don’t worry we are not going to be knitting!
I created a class to control an Access Line control and here’s a list of the properties, methods and events with descriptions.
|BottomRightAsCentre||False||Get/Set whether the rotation point uses the bottom, right end of the line (True). False uses the top, left position.|
|CentreX; CentreY||0; 0||Get the rotation point in twips.|
|Initialized||False||Get whether the Needle is correctly configured and therefore ready to move.|
|LastPosition||0||Get the previous position of the needle. Position is between 0 and the number of Steps.|
|Line||Nothing||Get/Set the Access Line control to use as a needle.|
|Position||0||Get/Set the position of the needle. When set the needle is moved to the new position.|
|Radius||Depends on Line Control used||Get the length (radius) of the needle in twips.|
|RangeEnd||0||In degrees the end of the range of movement.|
|RangeStart||0||In degrees the start of the range of movement.|
|Steps||1||The number of steps between the start and end range of movement.|
|Initialize||ByRef needleLine As Line,
ByVal startDegree As Double,
ByVal endDegree As Double,
ByVal numSteps As Long, Optional ByVal blnBottomRightAsCentre As Boolean = False
|Boolean||This is one method to configure the needle so it’s ready to move.
Dim fNeedle as Needle
If fNeedle.Initialize(Me.needleLine, 270, 90, 100, True) Then MsgBox(“Needle set up.”)
You can use With|End With and set the appropriate properties as an alternative to using the Initialize method. But remember to check the Initialized property is True before attempting to move it.
|Move||ByVal pos As Long||Moves the needle to the new position if it’s valid.|
|AfterMove||Fires after the needle has moved to its new position.|
|BeforeMove||Fires before the needle is moved to a new position.|
|LineChanged||Fires when the Line property has been set.|
The form above demos the needle class with 3 instances used as the hands of a clock. Albeit cool it’s not of much use as a dashboard, but it demonstrates that you could use the class to move Access Lines on the top of any graphic. OK Access flickers, so it isn’t usable for continuous movement but I hope you get the idea of what I’m trying to say. Anyway, here’s the code (excluding the Needle class) for the above form.
Option Compare Database Option Explicit Private fSecondHand As Needle Private fMinuteHand As Needle Private fHourHand As Needle Private Sub Form_Open(Cancel As Integer) ' Create new needle instances. Set fSecondHand = New Needle If Not fSecondHand.Initialize(Me.secondLine, 0, 360, 60) Then MsgBox "There was a problem setting up the second hand." Cancel = True End If Set fMinuteHand = New Needle If Not fMinuteHand.Initialize(Me.minuteLine, 0, 360, 3600) Then MsgBox "There was a problem setting up the minute hand." Cancel = True End If Set fHourHand = New Needle If Not fHourHand.Initialize(Me.hourLine, 0, 360, 43200) Then MsgBox "There was a problem setting up the hour hand." Cancel = True End If End Sub Private Sub MoveNeedle(ByRef hand As Needle, ByVal pos As Long) ' Move to position depending on seconds. hand.Move pos End Sub Private Sub Form_Timer() Dim numSeconds As Integer Dim minutesPos As Integer Dim numHours As Integer Dim hoursPos As Long ' Move clock hands every second. numSeconds = DatePart("s", Now()) MoveNeedle fSecondHand, numSeconds minutesPos = DatePart("n", Now()) * 60 + numSeconds MoveNeedle fMinuteHand, minutesPos numHours = DatePart("h", Now()) If numHours >= 12 Then numHours = numHours - 12 hoursPos = numHours * 3600# + minutesPos MoveNeedle fHourHand, hoursPos End Sub
As you can see, there’s not much code as most of the work is done by the Needle class. I won’t list it here because it’s quite long, so please download the demo TDS Dashboard v1.2.1. 188.42 kB and check it out.
We are using the Initialize method to create 3 instances of the Needle class. All have a range from 0 degrees to 360 degrees but with varying steps. Set for 1 step per second, the second hand takes 60 steps to rotate 360 degrees, the minute hand 3600 and the hour hand 43200.
We then call the MoveNeedle method for each instance of Needle in the form’s Timer function, which is set for every second (1000). The MoveNeedle method then moves the needle to its new position and we have an analogue clock working using normal Access Line controls.
Part 2 is now available.