With Access 2007 I believe all you have to do is set the control's Format
property to Percent. With earlier versions if you set the Format property to
Percent and the user enters the percentage sign after the value, that's
enough. If you want the user to be able merely to enter the value and not
the percentage sign, but have this appear in the control, then read on:
1. To prevent values less than 0% or over 100% from being entered put the
following in the control's BeforeUpdate event procedure:
Const conMESSAGE = _
"Percentage must be between 0 and 100"
Dim ctrl As Control
Set ctrl = Me.ActiveControl
If Not IsNull(ctrl) Then
' cancel update an inform user if value
' entered otside permitted range
If ctrl < 0 Or ctrl > 100 Then
Cancel = True
MsgBox conMESSAGE, vbExclamation, "Invalid operation"
End If
End If
2. In the AfterUpdate event procedure put:
Dim ctrl As Control
Set ctrl = Me.ActiveControl
If Not IsNull(ctrl) Then
' if user has entered percent sign remove it
' before converting value
ctrl = Replace(ctrl.Text, "%", "")
' divide control value by 100
ctrl = ctrl / 100
End If
3. Set the control's Format property to Percent.
Note that the value is stored as a fractional value so 10% is stored as 0.1,
but by virtue of the formatting you see it as 10%. You need to be aware of
this when using the value in any calculations. If for instance this is a
discount applied to a price then you'd compute the discounted price with:
DiscountedPrice = Price * (1- Discount)
which, if the price is 10 GBP say and the discount 10% would evaluate to:
10.00 * (1 – 0.1)
i.e. 10.00 – 0.9
which is 9.00 GBP.
Ken Sheridan
Stafford, England