i want that my text box accepts only numeric values. How do i do that? is there a "not in(0..9)" syntax, just like in Pascal?
Printable View
i want that my text box accepts only numeric values. How do i do that? is there a "not in(0..9)" syntax, just like in Pascal?
Here ya go:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> 8 Then KeyAscii = 0
End If
End Sub
'Numerics only please!
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Else
KeyAscii = 0
End Select
End Sub
thanks.
but what is the equivalent char of asc value 8?
You should add:
:rolleyes:You earn 1 face as a token.Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) Then Exit Sub
KeyAscii = 0
Beep
End Sub
Private Sub Text1_Change()
Dim i As Integer, s As String, t As String
For i = 1 to Len(Text1)
s = Mid(Text1, i, 1)
If Not IsNumeric(s) Then s = ""
t = t & s
Next
Text1.Text = t
End Sub
The Equal of Chr 8 is <-Backspace.