We help IT Professionals succeed at work.

field to allow only numerics with one exception

Fordraiders
Fordraiders asked
on
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

Comment
Watch Question

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
The Plus and Minus signs are considered 'numeric' ... if at the beginning of a number.

-2010201
+192818
Most Valuable Expert 2014

Commented:
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

Author

Commented:
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
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
-
Distinguished Expert 2017

Commented:
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.
Most Valuable Expert 2014
Commented:
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
Distinguished Expert 2017

Commented:
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.

Author

Commented:
nick67, Worked great !  I also added  "ASCII 46" for .   decimal

thanks

now i got another problem , but another question.

thanks !!

Author

Commented:
Thanks...
worked best
Distinguished Expert 2017

Commented:
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

Author

Commented:
Pat, I hope to pray folks don't enter that garbage, but you are right, you never know.
Thanks.

Author

Commented:
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.
Most Valuable Expert 2014

Commented:
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)