Yes!
Take a look at the attachment. This is a sample Speedometer gauge. The Heavy red line is a Line Object. Based on the Code, you must name this object "lnNeedle".
To use this speedometer, the following Code applies:
- Option Compare Database
-
Option Explicit
-
-
Const PI As Double = 3.14159265358979
-
Const dblRadius As Double = 1.75 * 1440
-
Const dblCtrH As Double = 2 * 1440
-
Const dblCtrV As Double = 2 * 1440
-
-
Private dblRatio As Double
-
Private dblRadians As Double
-
Private dblTop As Double
-
Private dblLeft As Double
-
Private dblHeight As Double
-
Private dblWidth As Double
-
-
Private Function UpdateSpeedometer(Current As Integer, Max As Integer)
-
On Error GoTo EH
-
If Current > Max Then Exit Function
-
dblRatio = Current / Max
-
dblRadians = dblRatio * 180 * PI / 180
-
dblTop = dblCtrV - (Sin(dblRadians) * dblRadius)
-
dblHeight = dblCtrV - dblTop
-
If dblRatio < 0.5 Then
-
Me.lnNeedle.LineSlant = False
-
dblLeft = dblCtrH - (Cos(dblRadians) * dblRadius)
-
dblWidth = dblCtrH - dblLeft
-
Else
-
Me.lnNeedle.LineSlant = True
-
dblLeft = dblCtrH
-
dblWidth = -Cos(dblRadians) * dblRadius
-
End If
-
Me.lnNeedle.Top = dblTop
-
Me.lnNeedle.Left = dblLeft
-
Me.lnNeedle.Height = dblHeight
-
Me.lnNeedle.Width = dblWidth
-
Exit Function
-
EH:
-
MsgBox "There was an error updating the Speedometer! " & _
-
"Please contact your Database Administrator.", vbOKOnly, "WARNING!"
-
Exit Function
-
End Function
Notes:
There are several constants:
PI: This is self-explanatory. Since MS Access does not have a constant for PI, we have to establish one.
dblRadius: This is the radius of the Line Control (the "needle"). Note that the radius of the Gauge is 2 inches (two inches high, four inches wide, but the Radius is two inches). For this example, we have set the radius to 1.75 inches (so it is not quite to the edge of the gauge).
dblCtrH and dblCtrV: These are the horizontal and Vertical center points of your Speedometer gauge. Remember, our gauge is two inches high and four inches wide. Because it is set all the way top/left, the center of the gauge (technically speaking, it is the vertex of the needle). Keep in mind that if you move the image to the right one inch, dblCtrH becomes three, and so on. This is important in terms of how the speedometer calculates points.
NB: Each of these three variables are multiplied by 1440. This is because MS Access uses "twips" when placing objects on the screen. A twip is 1/1440th of an inch. Once inch = 1440 twips.
The next variables are used within the calculations and are reasonably self-explanatory, as there will be a ratio of "completeness" that you want your speedometer to show, which will be converted into radians (not degrees, as MS Access uses radians for it's geometric functions).
We also need to know the Top, Left, Height and Width values for your line (the Needle), which will be calculated based upon the values you send to this function.
Of note, the values received by this function are integers, so if you need larger values, you can go to a Long Integer instead.
I did not add error trapping for this, but the assumption is that you will always be sending positive numbers to this function. you can easily add that error trapping into this module, or verify before you send values to the function.
This function does check whether the current is greater than the Max (we assume from 0 to 100%).
First, we calculate the ration of completeness. Then, calculate the radians. Certain mathematicians will say that
- dblRadians = dblRatio * 180 * PI / 180
should really be
- dblRadians = dblRatio * PI
which it could. However, we are only assuming a maximum travel of 180 degrees (the first 180). With a little extra programming you could have 360 dgrees of needle travel or 45--whatever you want. However, most of us think of a Speedometer as a half-circle (more or less). The second 180 is actually part of a formula to calculate Radians (Degrees * PI / 180). Your choice to keep it as is or change it.
The dblTop and dblHeight are always based on the Sine of the Radians, multiplied by the dblRadius of the Needle. This sizes the needle appropriately.
The If...Then checks to see if the needle is more than half way around the dial. If not, the line slants one way; if so, it slants the other. Then, we calculate the other values for the size and shape of the needle.
Finally, we assign our calculated values to the needle Line Object.
To use the Speedometer in your form, just call it as such:
- UpdateSpeedometer 10, 100
will indicate 10% complete.
Hope this hepps!