?
Solved

field to allow only numerics with one exception

Posted on 2016-08-30
13
Medium Priority
?
71 Views
Last Modified: 2016-08-30
Access 2010 vba  keypress event

I have an unbound textbox.
That only allows numerics.

I need to allow only one other specific characters  -  negative sign ?

Thanks
fordraiders


If (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii = 8) Then
      KeyAscii = KeyAscii
      Else:
      MsgBox ("You Must Enter Numbers Only!")
      KeyAscii = 0
End If

Open in new window

0
Comment
Question by:Fordraiders
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 75
ID: 41776808
The Plus and Minus signs are considered 'numeric' ... if at the beginning of a number.

-2010201
+192818
0
 
LVL 26

Expert Comment

by:Nick67
ID: 41776811
45 is the Ascii dash.
43 is the the Ascii +
I take it your code is a keypress event?
You aren't testing for isnumeric() so I assume the Change() event isn't in play
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 41776823
if i physically type in a negative or  plus sign...i get the message box..(which most people will do.  

Now if i copy and paste it will accept
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75
ID: 41776832
-
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 41776838
How are you restricting the textbox to allow only numbers?  It is better to use a numeric data type if these are truly numeric values.  As Joe already pointed out - the minus and plus signs are valid as the first character in numeric fields.
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 2000 total points
ID: 41776839
Select Case True 'always your best friend
    Case (KeyAscii > 47 And KeyAscii < 58)
    Case (KeyAscii = 8)
    Case (KeyAscii = 43)
    Case (KeyAscii = 45)
    Case Else
        MsgBox ("You Must Enter Numbers Only!")
        KeyAscii = 0
        Exit sub
End select
KeyAscii = KeyAscii
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 41776858
I just noticed that these also return true so if you want to restrict them to the first position, you will need to do that with code.

print isnumeric(+12-3)
True
print isnumeric(-12+3)
True

In all my years of using Access no user has ever put the plus or minus in the middle of the number.  It looks like Access goes along with the joke and adds or subtracts rather than balking.
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 41776861
nick67, Worked great !  I also added  "ASCII 46" for .   decimal

thanks

now i got another problem , but another question.

thanks !!
0
 
LVL 3

Author Closing Comment

by:Fordraiders
ID: 41776883
Thanks...
worked best
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 41776911
Since I showed that Access is OK with multiple + and - in a numeric field, you might want to modify the code to count instances.  Access even allows this:

print isnumeric(-12.1+3.1)
True
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 41776920
Pat, I hope to pray folks don't enter that garbage, but you are right, you never know.
Thanks.
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 41776931
ok, Pat, I thought , I thought about it. Yep, I'm dealing with Sellers, copying and pasting is a concern now that i think about it. They copy from excel sheets all day...hmmmm.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 41776943
That's why I asked about the Change event.
What you seem to have is a KeyPress event that looks at the fitness of any one keypress.

A Change event will look at the fitness of the presently-entered value.
They can be quite persnicketty, though.
You have to think of ALL the ways the user may use the box (like rubbing out previous values)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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, when working with VBA, learn some techniques for writing readable and easily maintained code.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

862 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