Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Allow a decimal place for  whole numbers

Posted on 2016-08-30
7
Medium Priority
?
44 Views
Last Modified: 2016-08-31
I had this question after viewing field to allow only numerics with one exception.

Access 2010 vba  keypress event and after update event

I have an unBound Textbox that allows only numeric characters, but also negative sign, plus sign, and a decimal.

The goal is that no matter what type of number is entered it keeps decimals and then tags a % sign at the end.

What I need:
If a person happens to enter  25%  or just 25
I need the result to add .00 for whole numbers
25%  becomes  25.00%
and
25 becomes  25.00%

Right now if i enter  22.22 it gives me correctly  22.22%
If i enter  22.00  it takes away the   .00   to 22%
I need it to keep 22.00 if its entered
AND ALSO
22% becomes  22.00 %

Current Code:
Private Sub ROLLING_12_GP_AfterUpdate()

' if they backspace and leave "" or null thats ok
If Len(Nz(Me.ROLLING_12_GP, "")) = 0 Then
   Exit Sub
End If
' temp take out the % (keypress does not allow) if they enter it
Me!ROLLING_12_GP = Replace([ROLLING_12_GP], "%", "")
' now add it back for
Me!ROLLING_12_GP = Me.ROLLING_12_GP.Value / 100 * 100 & "%"

' delete the % if they leave it
If Me!ROLLING_12_GP = "%" Then
   Me!ROLLING_12_GPP = ""
End If
End Sub


Private Sub ROLLING_12_GP_KeyPress(KeyAscii As Integer)
Select Case True 'always your best friend
    Case (KeyAscii > 47 And KeyAscii < 58)
    Case (KeyAscii = 8)
    Case (KeyAscii = 43)
    Case (KeyAscii = 45)
    Case (KeyAscii = 46)
    Case Else
        MsgBox ("You Must Enter Numbers Only!")
        KeyAscii = 0
        Exit Sub
End Select
KeyAscii = KeyAscii
End Sub

Open in new window



Thanks
fordraiders
0
Comment
Question by:fordraiders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 26

Accepted Solution

by:
Nick67 earned 2000 total points
ID: 41776998
Leave the % sign off
Format it with a custom format in the AfterUpdate

If IsNumeric(Me.ROLLING_12_GP) = True Then
    Me.ROLLING_12_GP.Value = Format(Me.ROLLING_12_GP.Value / 100, "0.00%")
End If

Nicely enough, this kills the math thing @PatHartman noted
Try 12.3-6.4
It doesn't format, so you can msgbox about invalid data then.
0
 
LVL 3

Author Comment

by:fordraiders
ID: 41777006
I'l take the code.
I took a simplier route.
Formatted the unbound textbox as "Percent"  which adds my % automatically.
"Decimals" to 2 places, which keeps my whole numbers !

Unless i'm not thinking it through ?
0
 
LVL 26

Expert Comment

by:Nick67
ID: 41777012
I thought about suggesting that.
It can depend on what the users enter

With textbox formatting '25' becomes '2500.00%'
That can be annoying.
If you are looking at percents and entering them, not needing to do the head-math, but straight entry is nice.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:fordraiders
ID: 41777027
but that does not work for my subform with fields that are bound ...uggggh !
the data type changes that is...so now i'm back to code on the subform.
these field are text...
0
 
LVL 3

Author Comment

by:fordraiders
ID: 41777040
welll kudos to you and Pat!!. Should have given Pat some Points.
But your above code worked great !!
in the subform !!.

Thanks
Nick !  and Pat.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 41777061
<grin>
What you display and what you store do not have to be, and generally are not the same thing, my son.

Save as a single, decimal or fixed or text but display as a percentage -- as you can see alters stuff.
Store '25' and display it as 25.00%
or store '25.00%' and display it as-is.

A lot of folks get unhappy when they ask questions about UI and get answers about the data design -- but data design is the arse-biter.

Other ways to play is to put a transparent control with one on top of the other.
One control displays niceness while the other actually gets the focus and permits entry and editing handled by GotFocus/LostFocus events.

I don't know that @PatHartman looked at this one.
You can go back to the other one and 'Request Attention' and ask to re-open the Q to re-assign pointsRA
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 41778049
thanks
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question