Hi
I am trying to write a code whereby user can only input integer values in a text box. If user were to input non integer values then the validate event will be set to false and a msg box will be displayed asking user to enter an integer value
Code as follows :
Private Sub Text1_Validate(Cancel As Boolean)
Dim x, y As Integer
For x = 1 To Len(Text1.Text)
For y = 1 To 255
If Chr$ = Mid$(Text1.Text, 1, x) Then
If y < 48 Or y > 57 Then
Cancel = True
MsgBox "You must enter an integer"
Text1.Text = ""
End If
End If
Next y
Next x
End Sub
Problem is there’s an error in this code and I can’t seem to figure out what it is.
Say I input character “a” in the text box it will prompt for the msg box
But if I input an integer in front of the character a i.e 2a then the validate event does not reject it. Why ? It would seem that the above code is checking only the first character that appears in the text box and not the rest
Anybody can help out
Thanks
I am trying to write a code whereby user can only input integer values in a text box. If user were to input non integer values then the validate event will be set to false and a msg box will be displayed asking user to enter an integer value
Code as follows :
Private Sub Text1_Validate(Cancel As Boolean)
Dim x, y As Integer
For x = 1 To Len(Text1.Text)
For y = 1 To 255
If Chr$ = Mid$(Text1.Text, 1, x) Then
If y < 48 Or y > 57 Then
Cancel = True
MsgBox "You must enter an integer"
Text1.Text = ""
End If
End If
Next y
Next x
End Sub
Problem is there’s an error in this code and I can’t seem to figure out what it is.
Say I input character “a” in the text box it will prompt for the msg box
But if I input an integer in front of the character a i.e 2a then the validate event does not reject it. Why ? It would seem that the above code is checking only the first character that appears in the text box and not the rest
Anybody can help out
Thanks