I don’t want the program to accept letters

Asked By 10 points N/A Posted on -

I am creating a visual basic project and I don't want the program to accept input if the user input a letter. I want only numbers to be accepted. In addition, If the user enter a letter or not a whole number the program will return a message stating "Please enter a valid number" I just need a sample code to serve as my reference. please give me some clue on how to code this.


Answered By 0 points N/A #102049

I don’t want the program to accept letters


ASC key values are integer values that are assigned to every symbol that we come across the symbol and hidden characters. You will find the ASC value for A is 65 and keeps on increasing till Z and a is 97 and keeps on increasing till z.

The programming logic is built accordingly.
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= Asc("A") And KeyAscii <= Asc("Z") And KeyAscii >= Asc("a") And KeyAscii <= Asc("z") Then
KeyAscii = 0
MsgBox ("enetr valid data")
End If
End Sub

The above code will not allow a text control named Text1 to accept the input if it is a character, from A to Z and from a to z. 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.


Login/Register to Answer

Related Questions