Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form control to show an offset left/right of center??

Status
Not open for further replies.

Smoothcat74

Technical User
Aug 29, 2008
40
0
0
Hi everyone,

I am wondering if there is such a thing as a graphical control that shows how much offset you have between a center value. Kind of a like a progress bar that starts in the middle of the control and fills either left or right of the line depending on the value feeding the control.

Essentially, I have a value that will show as less or greater than 100% and I would love to illustrate that offset graphically.

Does such a thing exist?
 
If this was a report, you would have more options. However, on a form, you can use a text box with these properties:
Control Source: =String([YourNumericValue],"g")
Fore Color: 255
Font Name: Webdings
Special Effect: Flate
Back Style: Transparent
Locked: Yes
Enabled: No

You may need to adjust the font size and text box width to display the full line.



Duane
Hook'D on Access
MS Access MVP
 
None that I know of, but you can roll your own. This really demonstrates the power of using custom classes in vba.

To make this work you need to drop on any form
1)a label to be the background for your offset graph
2)a label to be the shaded region of your offset graph. I call it the face
3) a line to be the center point of your graph
4) a text box bound to your value.

This is the only code you need on your form to make this work. And that is the power of using custom classes.
Code:
Public osgPercent As OffSetGraph

Private Sub Form_Load()
  Set osgPercent = New OffSetGraph
  osgPercent.initOSG Me, Me.lblBack, Me.lblFace, Me.lnCenter, Me.txtBxValue, 0, 2
End Sub
The parameters are
the form (me)
the background label
the face label
the line
the text box that has the value
minimum value for the graph (default 0)
maxvalue for the graph (default 2/200%)



Only the background label needs to be properly located and sized, the code will properly format the line and the label for shading.

everything else is contained in the class module which can by used on any form. The code has to go in a class module not a standard module, and it has to be named OffsetGraph.

class OffSetGraph

Code:
Option Compare Database
Option Explicit

    Private WithEvents mForm As Access.Form
    Private mLblBack As Access.Label
    Private mLblFace As Access.Label
    Private mMinValue As Long
    Private mMaxValue As Long
    Private mWidth As Single
    Private mHeight As Single
    Private mCenterLocation As Single
    Private mLine As Access.Line
    Private mValueTextBox As Access.TextBox
Public Sub initOSG(theForm As Access.Form, LabelBack As Access.Label, LabelFace As Access.Label, CenterLine As Access.Line, ValueTextBox As Access.TextBox, Optional minValue As Long = 0, Optional maxValue As Long = 2)
  Set mForm = theForm
  Set mLblBack = LabelBack
  Set mLblFace = LabelFace
  Set mLine = CenterLine
  Set mValueTextBox = ValueTextBox
  mWidth = LabelBack.Width
  mMinValue = minValue
  mMaxValue = maxValue
  mCenterLocation = mWidth / 2
  formatLabelBack
  formatLabelFace
  formatCenterLine
  mForm.OnCurrent = "[Event Procedure]"
End Sub
Public Sub formatLabelBack()
  With mLblBack
    'Sunken
    .SpecialEffect = 2
    .BorderWidth = 3
    .BackColor = vbWhite
    .Caption = ""
    'normal
    .BackStyle = 1
  End With
End Sub
Private Sub formatLabelFace()
  With mLblFace
    'Raised
    .SpecialEffect = 0
    .BackColor = vbBlue
    .Caption = ""
    .BackStyle = 1
    .Height = mLblBack.Height
    .Top = mLblBack.Top
    .Left = mLblBack.Left
  End With
End Sub
Private Sub formatCenterLine()
  With mLine
    .Height = mLblBack.Height
    .Top = mLblBack.Top
    .Left = mLblBack.Left + mCenterLocation
    .BorderWidth = 3
  End With
End Sub
Private Sub mForm_Current()
  locateBar
End Sub
Private Sub locateBar()
 Dim currentValue As Single
 Dim fractionSize As Single
 currentValue = Nz(mValueTextBox.Value)
 If currentValue >= 1 And currentValue <= mMaxValue Then
   fractionSize = (currentValue - 1) / (mMaxValue - 1)
   mLblFace.Width = fractionSize * (mWidth / 2)
   mLblFace.Left = mLblBack.Left + mCenterLocation + mLine.BorderWidth / 2
 ElseIf currentValue < 1 And currentValue >= mMinValue Then
   fractionSize = (1 - currentValue) / (1 - mMinValue)
   mLblFace.Width = fractionSize * (mWidth / 2)
   mLblFace.Left = mLblBack.Left + mCenterLocation - mLine.BorderWidth / 2 - mLblFace.Width
 ElseIf currentValue > mMaxValue Then
   mLblFace.Width = (mWidth / 2)
   mLblFace.Left = mLblBack.Left + mCenterLocation + mLine.BorderWidth / 2
   MsgBox "Exceed Max Value"
 ElseIf currentValue < mMinValue Then
   mLblFace.Width = (mWidth / 2)
   mLblFace.Left = mLblBack.Left + mCenterLocation - mLine.BorderWidth / 2 - mLblFace.Width
   MsgBox "Min value exceeded"
 End If
End Sub

Here is a working demo. I recycled this from some code that shaded a timeline.

Pretty slick, I have to admit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top