Solved

field to allow only numerics with one exception

Posted on 2016-08-30
13
34 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
 
LVL 75
ID: 41776832
-
0
 
LVL 34

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 500 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 34

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 34

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now