Does anyone have any suggestions on how to check a textbox to make sure it contains only numbers?
------------------
Ryan
[This message has been edited by Gimpster (edited 02-14-2000).]
Printable View
Does anyone have any suggestions on how to check a textbox to make sure it contains only numbers?
------------------
Ryan
[This message has been edited by Gimpster (edited 02-14-2000).]
there is the IsNumeric function...
or
If str(val(text1.text)) = str(text1.text) then [it's only numbers...] but never tested it...
or just verify every keypress to the textbox and make sure it is a digit 0-9
------------------
Rapmaster
Gimpster,
the function "IsNumeric()" works, but recently i read about the "Like" function in a book which i found useful in my projects specially when i need string validations.
Personally I check that it's a number on the LostFocus event - you can catch the keypresses etc in the KeyDown, Change or Keypress event but you have to allow for the copy/pasting, delete key etc etc etc.
Textbox1_LostFocus
If Not IsNumeric(TextBox1.Text) Then
Msgbox "Must enter a number!"
TextBox1.SetFocus
End If
End Sub
This will prevent the user from moving onto the next control if they don't enter a number.
If you want to allow the user to leave the textbox blank you should insert
If TextBox1.Text="" Then Exit Sub
immediately below the TextBox1_LostFocus line (ie, before the IF)
------------------
Mark "Buzby" Beeton
VB Developer
[email protected]