Determine if input is a decimal

F

Fred Boer

Hello!

I have a student creating a form. He wants to use an AfterUpdate event to do
data validation. He wants only whole numbers, no decimals. He wants help
with this and expects I know how to do this.... I said *of course* I could,
but that I had no time just then... ;)

I suppose I might try doing something like searching for a period using
InStr() or something, but perhaps there is a more elegant "math" function or
some kind of "math" thing that can determine if a number has decimals?

Note: When you read "math", invest it with all the fear, loathing,
bewilderment and consternation you can - that's the way I say it! ;)

Thanks!
Fred Boer
 
J

Jim Shaw

You could try:
if int(myData) = MyData then
'input is an integer
else
'input is real
endif
Jim
 
K

Ken Snell [MVP]

If Me.ControlName.Value <> CLng(Me.ControlName.Value) Then
MsgBox "You must enter a whole number!"
End If


Note that this will work so long as the number is within the data size
limits of a Long data type. If the number will be larger than those limits,
then you may need to use InStr to search for embedded decimal point.
 
F

Fred Boer

Thanks, Ken and Jim!

Fred

Ken Snell said:
If Me.ControlName.Value <> CLng(Me.ControlName.Value) Then
MsgBox "You must enter a whole number!"
End If


Note that this will work so long as the number is within the data size
limits of a Long data type. If the number will be larger than those
limits,
then you may need to use InStr to search for embedded decimal point.
 
M

Marshall Barton

Fred said:
I have a student creating a form. He wants to use an AfterUpdate event to do
data validation. He wants only whole numbers, no decimals. He wants help
with this and expects I know how to do this.... I said *of course* I could,
but that I had no time just then... ;)

I suppose I might try doing something like searching for a period using
InStr() or something, but perhaps there is a more elegant "math" function or
some kind of "math" thing that can determine if a number has decimals?


If it must consist of only digits, you can check it with:

If Me.textbox Like "*[0-9]*" Then
' Not an integer
End If

If it can be negative, you can check for the minus sign
first by using Left(me.textbox, 1) = "-"

If you want to allow entries like 123.00 or 12.3E1, then use
the Int, Fix or CInt techniques suggested by others, but be
sure to use error handling to catch the case where a user
enters text such as ABC.
 
F

Fred Boer

Thanks for yet another technique, Marsh! I'll suggest them all and dazzle
the kid into thinking his teacher is *really* sharp! ;)

Fred

Marshall Barton said:
Fred said:
I have a student creating a form. He wants to use an AfterUpdate event to
do
data validation. He wants only whole numbers, no decimals. He wants help
with this and expects I know how to do this.... I said *of course* I
could,
but that I had no time just then... ;)

I suppose I might try doing something like searching for a period using
InStr() or something, but perhaps there is a more elegant "math" function
or
some kind of "math" thing that can determine if a number has decimals?


If it must consist of only digits, you can check it with:

If Me.textbox Like "*[0-9]*" Then
' Not an integer
End If

If it can be negative, you can check for the minus sign
first by using Left(me.textbox, 1) = "-"

If you want to allow entries like 123.00 or 12.3E1, then use
the Int, Fix or CInt techniques suggested by others, but be
sure to use error handling to catch the case where a user
enters text such as ABC.
 

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