Testing for numeric input

J

John F.

Hi:

I'd like to ensure that all input into a textbox, 'tboxNumber', is numeric
and/or a decimal point. The input must be text, not a numeric type.

How is that best done? The number has to have at least 2 digits to the left
of the decimal, but can have up to 3 digits on either side of the decimal
point. It does not have to have a decimal point; if there is a decimal there
can be 0 - 3 digits after the decimal.

Thank you.

johno
 
A

Allen Browne

By "2 digits to the left", I assume you mean the number must be at least 10.
The "3 digits to the right" is not reallly meaningful for floating point
data types, and you don't mention negatives, so perhaps you could try
setting these properties:
Format: General Number
Validation Rule: >= 10
 
J

John F.

Thanks, Allen.

To clarify: It's actually a code, not a 'number' as such:

"15" would be OK, "15." would not; "15.0" would be OK, "1.78" would not (has
to have 2 digits to the left of the decimal).

And it all has to be text, not a number. In C#, as I recall, there's a 'not
a number' function or you can try to convert the text into a numeric value
and if that fails, that flags the textbox value as invalid. Nothing like that
in Access VBA, I guess.

I was thinking about testing each key-down to see if it was numeric or a
decimal point but that's beyond my skill. If I know everything that's entered
meets that basic test and I'm assured that users aren't entering alphabetic
characters, I could live with that.

Best,

johno
 
A

Allen Browne

You can use KeyPress to respond to a character, or Change to examine what
the Text property is in the control.

You can use IsNumeric() to test if it is a number, but 15. would return
True.

You may be able to use an input mask to indicate what digits are required,
but I think you would have problems with the decimal.

If you use the Change event, use InStr() to locate the decimal point, and
Left() or Mid() to parse the preceeding and following characters from the
Text property. Len() gives the total number of characters.
 
J

John F.

Thank you, Allen. I'll work with these functions and come up with something.

Best,

johno.

PS I hope it's a reasonable hour in Perth: my brother lives there and it's
always a challenge to remember when to call from the US west coast and not be
waking him up on 'your' west coast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top