Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for numeric data in a field. 2

Status
Not open for further replies.

dctechuser

Technical User
Sep 11, 2009
9
US
I have a Work Order Number field that is called Workorder_TextBox. This Workorder_ Textbox field can be left blank and the record can save, but when something is entered in the Workorder_textbox field, I want it to give a message that only numbers are allowed. If some user comes in and puts abcderrr in there, I do not want the record to save but have the user get a message and then exit the sub so as to make the correction.

How can I write VB coding to do this? I need to allow blanks, because records can be added if the user has no number available, but when a number is available, I want only numbers entered and so I want an edit check for this to not allow a saved record to occur if user enters letters.

 
Should always check the FAQ faq796-3369. I've not used it but from what I've heard it works well. You can check a string with IsNumeric(). I've heard some things that are not truly numeric can get through, but I've not had any problems with it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
IsNumeric returns true for a string that can be converted to ANY numeric type. This includes scientific notation, so "1e2" returns true. There are things you can do to trick IsNumeric in to returning true only for common numbers. For example...

Disallow scientific notation
IsNumeric(YourString & "e0")

Disallow fractional numbers
IsNumeric(YourString & ".0e0")

Disallow negative numbers
IsNumeric("-" & YourString & "e0")

(my favorite)

Only allow positive integers
IsNumeric("-" & YourString & ".0e0")

On thing to be careful of.... Some of these tests will return true for empty strings. For example:

IsNumeric("e0") returns False
IsNumeric(".0e0") returns true
IsNumeric("-.0e0") returns true

If you are writing validation code such that you must enter a positive integer, then you'll need to check for empty string separately.

Of course.... I'm told that Integer.TryParse works pretty well too.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top