Visual Basic Code to disable Special Characters

Asked By 0 points N/A Posted on -
qa-featured

Hello experts.

I really need your help on this one. I have been creating a VB program and all its left are the trappings on it.

I wanted to disable the special characters to be inputted on the text field to avoid mismatch errors.

What would be the code to disable the special characters?

Hoping for your answer.

Thanks

SHARE
Answered By 0 points N/A #100995

Visual Basic Code to disable Special Characters

qa-featured

Private Sub Text1_KeyPress(KeyAscii As Integer)

 
If KeyAscii >= Asc("A") And KeyAscii <= Asc("Z") And KeyAscii >= Asc("a") And KeyAscii <= Asc("z")  and KeyAscii >= Asc("0") And KeyAscii <= Asc("9")  Then
 
else
 
KeyAscii = 0
 
MsgBox ("enter valid data")
 
End If
 
End Sub
 
An asc key is a value assigned to every individual symbol assigned to be used in the current system. the asc keys are normally numbered 256. not for Unicode. means a total of 256 symbols.
The above code will not allow a text control named Text1 to accept the input if it is not a character or not a numeric. It accepts from A to Z and from a to z and 0 to 9. Under key press event of the text box, you add the above code. Change the message as desired, but do not change the if statement for the given condition.
 
KeyAscii=0 means that the input from keyboard will not be displayed. You may need to allow the ENTER and backspace keys in the If statement.
 
Thanks.
 
If you have your answer then please comment.
 

Related Questions