HomeNewsProducts & ServicesDownloadsAccess TutorialsContact UsCheckout  View Cart
Tutorial Tools
Search
Related Tutorials
Tutorial Categories

Enter value as a percent

Category:FormsUser Rating:**** (1 vote)
Applies to:Access 95 and aboveViews:1,736
User Level:EasyYour Rating:Your rating: PoorYour rating: Not so goodYour rating: AverageYour rating: GoodYour rating: Excellent
Author:Allen BrowneUser Comments:0   Have your say...
Revision:1.0
Date:31/05/2005Bookmark & share:Bookmark with Del.icio.us Digg This! Post to Reddit Share on Facebook Post to StumbleUpon Bookmark with Yahoo Bookmark with Google 

SmartAccess

Advertisement - The Smart Access Magazine is back!

Over 300 articles by over 100 Access professionals

Exclusive 10% discount for EverythingAccess.com readers:  coupon EA-45K2D46TVS









Bookmark & share:
Bookmark with Del.icio.us Del.icio.us
Digg This! Digg This!
Post to Reddit Reddit
Share on Facebook Facebook
Post to StumbleUpon StumbleUpon
Bookmark with Yahoo Yahoo
Bookmark with Google Google

Sign up for our free e-mail newsletter to receive our latest tutorials by e-mail


MS-Access Tips for Casual Users

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:

  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:
        =MakePercent([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

Please Note: This is a cached tutorial - last updated 18/05/2009.
To view the tutorial at its original location click here: http://www.allenbrowne.com/casu-16.html



Have your say - comment on this article.
What did you think of 'Enter value as a percent'?


What others thought...

No comments yet.

Be the first to comment on this article!

Have your say...
Name
E-mail (e-mail address will not be shown on the website)
Comments
Verification CodeVerify Code
 
Please note:It may take 30 seconds after clicking submit - please be patient to avoid duplicate submissions. 
All comments are approved before being displayed on this web page.  This process may take up to 24 hours.


© 2008 iTech Masters. All rights reservedAccess Database RepairTerms of serviceLinksNewsletter