Hi,

I want keypress event sample code.. I have a textbox and I want only numbers to be allowed inside the textbox.. help me out plz..thanks in advance.

I tried:

Private Sub Text1_KeyPress(KeyAscii As Integer)

If (58 > KeyAscii > 47) Then

Else

MsgBox "Enter only Numbers"

Text1.Text = ""

End If

End Sub

But this din’t work.. plz help me out

Hi,

change the above code like this

Private Sub Text1_KeyPress(KeyAscii As Integer)

   'If ( KeyAscii  > 47 And KeyAscii < 58) Then
   If ( IsNumeric ( Chr ( KeyAscii  ) ) Then
    
   Else
       MsgBox "Enter only Numbers"
       'Make sure that event is processed
       KeyAscii = 0

    End If
End Sub

But u can use some other better logic to check numeric or not.

It will prevent the control chars too (Backspace, Delete ..)

Thanks :) Can u give me the code equivalent to the following line for alphabets and special characters also..? thanks in advance
If ( IsNumeric ( Chr ( KeyAscii ) ) Then


Hi,

change the above code like this

Private Sub Text1_KeyPress(KeyAscii As Integer)

   'If ( KeyAscii  > 47 And KeyAscii < 58) Then
   If ( IsNumeric ( Chr ( KeyAscii  ) ) Then
    
   Else
       MsgBox "Enter only Numbers"
       'Make sure that event is processed
       KeyAscii = 0

    End If
End Sub

But u can use some other better logic to check numeric or not.

It will prevent the control chars too (Backspace, Delete ..)

Thanks :) Can u give me the code equivalent to the following line for alphabets and special characters also..? thanks in advance
If ( IsNumeric ( Chr ( KeyAscii ) ) Then

Sure, I can give some alternatives,before that if u have a time, u try your own..

Try it.
>>Controls characters are within the range 0 to 32..
You should dont restrict the key ascii between this range.

>>And numeric keys reside in the range 47 to 58
Your code

If (58 > KeyAscii > 47) Then

change as

If (KeyAscii > 47 And KeyAscii < 58 ) Then

>>Also allow user to enter +, ., - chars

try this logic inside the code.

Private Sub Text4_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= 32 Or (KeyAscii >= 91 And KeyAscii <= 96) Or KeyAscii >= 123) Then
KeyAscii = 0
MsgBox "Only Special Characters are allowed"
End If
End Sub

i tried the above code for validation of special characters. My text box should allow only special characters. But with the above code, my text box allows all types of characters :( plz help

Thanks in advance

My text box should allow only special characters.

First you find the ascii for the special characters then try to code it ..

Ex:

0-32 Allowed
91-96 Allowed
>= 123 Allowed then

If (  (KeyAscii >= 0 And KeyAscii <= 32 ) Or ( KeyAscii >= 91 And KeyAscii <= 96 ) Or ( KeyAscii >= 123 ) ) Then
   'These keys are allowed

Else
   'These Chars not allowed

End If

Yep... i have corrected my code. :) thanks a lot for your valuable suggestion :)
Here is my corrected code :)

Private Sub Text1_KeyPress(KeyAscii As Integer)
'If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
If Not (IsNumeric(Chr(KeyAscii))) Then
KeyAscii = 0
MsgBox "Only numbers are allowed"
End If
End Sub


Private Sub Text2_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= 97 And KeyAscii <= 122) Then
KeyAscii = 0
MsgBox "Only lower case alphabets are allowed"
End If
End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= 65 And KeyAscii <= 90) Then
KeyAscii = 0
MsgBox "Only Upper case alphabets are allowed"
End If
End Sub


Private Sub Text4_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 32 And KeyAscii <= 47) Or (KeyAscii >= 91 And KeyAscii <= 96) Or (KeyAscii <= 58 And KeyAscii >= 64) Or (KeyAscii <= 123 And KeyAscii >= 126)) Then
KeyAscii = 0
MsgBox "Only Special Characters are allowed"
End If
End Sub


Private Sub Text5_KeyPress(KeyAscii As Integer)
If (KeyAscii >= 0 And KeyAscii <= 31) Then
KeyAscii = 0
MsgBox "You have entered some Alt key"
End If
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.