Provided by Allen Browne, May 2005. Updated May 2006.
Enter value as a percent
Update: In Access 2007, if a field or control is formatted as Percent, the % is added automatically. This code is unnecessary.
When you set a field's Format property to "Percent" and enter 7, Access interprets it as 700%.
How do you get it to interpret it as 7% without having to type the percent sign for every entry?
Use the AfterUpdate event of the control to divide by 100.
But then if the user did type "7%", your code changes it to 0.07%.
We need to divide by 100 only if the user did not type the percent sign.
To do that, examine the Text property of the control.
Unlike the control's Value, the Text property is the text as you see it.
(In Access, unlike pure VB, the Text is available only for the control that has focus.)
Using the code
To save the function in your database:
- Choose the Modules tab of the Database window.
- Click New to open a module.
- Copy the code below, and paste into your module.
- Save the module with a name such as "Module1".
To apply to a text box named "Text23":
- Open the form in design view.
- Right-click the text box, and choose Properties.
- Set the After Update property of the text box to:
=MakePercent([Text23])
Public Function MakePercent(txt As TextBox)
On Error GoTo Err_Handler
If Not IsNull(txt) Then
If InStr(txt.Text, "%") = 0 Then
txt = txt / 100
End If
End If
Exit_Handler:
Exit Function
Err_Handler:
If Err.Number <> 2185 Then
MsgBox "Error " & Err.Number & " - " & Err.Description
End If
Resume Exit_Handler
End Function