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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing a textbox entry for numbers only 1

Status
Not open for further replies.

MikeCt

Programmer
Nov 6, 2001
44
US
Hi
Is there a way to test if a entry into a textbox contains only numbers, I would like to stay away from using a Masked
edit box if possible.

Private Sub Tbox_Price_LostFocus()
If Tbox_Price <> ???? then
Msgbox("Invalid entry, Please enter numbers only")
end if
End Sub

Thank
Mike
 
Well there is the intrinsic IsNumeric Function that you could use to test the contents of the text box, or you could just prevent the user from entering anything that is not a number...
[tt]
Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = vbKeyBack Then Exit Sub
If KeyAscii = vbKeyTab Then Exit Sub

If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then
Else
KeyAscii = 0
End If

End Sub
[/tt]

However, this will not stop the user from right clicking on the text box and pasting text in, but with the use of both the code above and the intrinsic IsNumeric Function in the Lost Focus Event of the text box, you should be pretty well covered...



Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top