VB 6.0 - How to only allow numeric values in the TextBox

In this free tutoril Visual Basic 6.0 we will share about How to only allow numeric values in the TextBox VB 6.0. In this tutorial usually use if the data numeric to save into database.

For basic code you can using below code :
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = vbKeyBack Or KeyAscii = vbKeyReturn) Then KeyAscii = 0
End Sub
 For example, you can design form in VB 6.0 like below picture

 In Price (Text3) and Quantity (Text4) we will validate only numeric can input data in Text, so you can insert below code in Form1 :
 Private Sub Form_Load()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = vbKeyBack Or KeyAscii = vbKeyReturn) Then KeyAscii = 0
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = vbKeyBack Or KeyAscii = vbKeyReturn) Then KeyAscii = 0
End Sub
After that, please run your VB 6.0 Project :)

Related Posts :

0 Response to "VB 6.0 - How to only allow numeric values in the TextBox"