Checking for numeric value

  • Thread starter Thread starter Simon Harris
  • Start date Start date
S

Simon Harris

Hi,

I want eo ensure that my users only enter numeric values into a text box.
I've tried this, as a starting point, which always returns 'false' whether I
enter a number or not.

Private Sub hours_spent_Change()
MsgBox (IsNumeric(Me!hours_spent))
End Sub

I would have expected this to be quite a common task - 30 minutes searching
google etc didnt return much of any use.

Hope some one can help.

Kind regards,

Simon.


--
-
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!
 
Your code works ok for me. Have you tried moving it to another event?
Perhaps After Update?

hth,
 
First thought: Make the underlying field in the
table "Number."

If there is a reason for not doing that, then in the
Validation Rule for hours_spent text box enter this code:
=IsNumeric([hours_spent]). When leaving the field the
user will get an error message. If you want a better
error message than Access provides, then type your own in
the Validation Message property.

Roxie Aho
 
I want eo ensure that my users only enter numeric values into a text box.
I've tried this, as a starting point, which always returns 'false' whether I
enter a number or not.

Private Sub hours_spent_Change()
MsgBox (IsNumeric(Me!hours_spent))
End Sub

I would have expected this to be quite a common task - 30 minutes searching
google etc didnt return much of any use.

You are trying to compare to the "Value" of the control, but the "Value"
property doesn't include what you type while you're still typing it. You could
check for a numeric value, instead, in the control's "BeforeUpdate" event
procedure (where you could "Cancel" the update if the value isn't numeric)

'****EXAMPLE START
Private Sub txtMyTextBox_BeforeUpdate(Cancel As Integer)

If Not IsNumeric(Nz(Me.txtMyTextBox.Value, 0)) Then
MsgBox "Not a Numeric value!"
Cancel = True
Else
'Entry is numeric, so accept it
End If

End Sub
'****EXAMPLE END

.... or you could prevent the entry of *alpha* characters while the user is
typing in a value using the "KeyPress" event procedure:

'****EXAMPLE START
Private Sub txtMyTextBox_KeyPress(KeyAscii As Integer)

If UCase(Chr(KeyAscii)) >= "A" And UCase(Chr(KeyAscii)) <= "Z" Then
'A "letter" key was pressed, so
' send the key's value to "the void"
KeyAscii = 0
Else
'The key pressed was not a "letter" key, so
' let the key's value pass through
End If

End Sub
'****EXAMPLE END
 
Hi,

Thanks for your suggestions - Cheryl, I tried with and with out .value, both
return 'false'

The text box is not bound to any table or query, its input only, the update
is called from a button.

I'm guessing its always going to be false, because its a text box, there
fore a string. Perhaps I need to do a test to turn the value into an integer
first? if that fails, its not a number.

Anyone got any ideas?

Simon.

Roxie Aho said:
First thought: Make the underlying field in the
table "Number."

If there is a reason for not doing that, then in the
Validation Rule for hours_spent text box enter this code:
=IsNumeric([hours_spent]). When leaving the field the
user will get an error message. If you want a better
error message than Access provides, then type your own in
the Validation Message property.

Roxie Aho
-----Original Message-----
Hi,

I want eo ensure that my users only enter numeric values into a text box.
I've tried this, as a starting point, which always returns 'false' whether I
enter a number or not.

Private Sub hours_spent_Change()
MsgBox (IsNumeric(Me!hours_spent))
End Sub

I would have expected this to be quite a common task - 30 minutes searching
google etc didnt return much of any use.

Hope some one can help.

Kind regards,

Simon.


--
-
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!


.
 
Wonderful stuff - Thanks Bruce, used the keypress method, which works a
treat.

Thanks again,

Simon.
 
Simon Harris said:
Hi,

I want eo ensure that my users only enter numeric values into a text
box. I've tried this, as a starting point, which always returns
'false' whether I enter a number or not.

Private Sub hours_spent_Change()
MsgBox (IsNumeric(Me!hours_spent))
End Sub

I would have expected this to be quite a common task - 30 minutes
searching google etc didnt return much of any use.

Hope some one can help.

Kind regards,

Simon.

In addition to the other solutions presented, note that if you set the
text box's Format property to one of the numeric format, users won't be
able to enter non-numeric data.
 
Back
Top