MS-Access Tips for Casual Users

Provided by Allen Browne, May 2005.  Last updated: April 2010.

Enter value as a percent

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.)

Using the code

To save the function in your database:

  1. Choose the Modules tab of the Database window.
  2. Click New to open a module.
  3. Copy the code below, and paste into your module.
  4. Save the module with a name such as "Module1".

To apply to a text box named "Text23":

  1. Open the form in design view.
  2. Right-click the text box, and choose Properties.
  3. Set the After Update property of the text box to:

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 Function

    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