"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:XL**************@news-binary.blueyonder.co.uk...
When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind
of thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.
Several ways.. I usually tap into the keypress routine so assuming you have
a text box, here is how the routine will look:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Call num(KeyAscii)
End Sub
Finally the routine to check for numeric only (plus I added sample for
period (in case decimal, and also backspace). Any other gets ignored:
Private Sub num(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
KeyAscii = IIf(Chr$(KeyAscii) Like "[0-9]", KeyAscii, 0)
End Sub