Provided by Allen Browne, May 2005. Last updated: April 2010.
Note: In Access 2007 and later, if a field or control is formatted as Percent, the % is added automatically. This code is unnecessary (though it won't cause problems.)
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.)
To save the function in your database:
To apply to a text box named "Text23":
Public Function MakePercent(txt As TextBox) On Error GoTo Err_Handler 'Purpose: Divide the value by 100 if no percent sign found. 'Usage: Set the After Update property of a text box named Text23 to: ' =MakePercent([Text23]) 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 'No Text property unless control has focus. MsgBox "Error " & Err.Number & " - " & Err.Description End If Resume Exit_Handler End Function
Home | Index of tips | Top |