hello can you help me please.. i have this trapping that will trap the characters..but my problem my code is too long..are there other ways to shorten my code..please help me ...hoping for your positive responds...

Public Function mchnotrap(KeyAscii As Integer)
If KeyAscii >= 33 And KeyAscii <= 44 Or KeyAscii = 45 Or KeyAscii = 46 Or KeyAscii = 47 Or KeyAscii = 63 Or KeyAscii = 64 Then
  KeyAscii = 0
  MsgBox "Invalid input only numbers and letters are allowed", vbInformation, "Invalid, Input...Input Again merchandise number"
  Exit Function
  End If
If KeyAscii >= 58 And KeyAscii <= 62 Or KeyAscii >= 91 And KeyAscii <= 96 Then
   KeyAscii = 0
   MsgBox "Invalid, input only numbers and letters are allowed", vbInformation, "Invalid Input...Input Again merchandise number"
   Exit Function
   End If
 If KeyAscii >= 123 And KeyAscii <= 127 Then
   KeyAscii = 0
   MsgBox "Invalid, input only numbers and letters are allowed ", vbInformation, "Invalid Input...Input Again merchandise number"
   Exit Function
   End If
End Function

Private Sub txtmchno_KeyPress(KeyAscii As Integer)
Call mchnotrap(KeyAscii)
End Sub

Jemz,

As below. You do not really have to notify a user with a message box everytime they press the wrong key. Rather make a small label note next to the text box or the top of your form etc, notifying them that only text and numbers is accepted.

You also might want to add some code to this for the user to use the Backspace, Delete and Enter keys. Their respective Ascii numbers is Enter = 13, Delete = 127 , Backspace = 8.

Option Explicit

Public Function mchnotrap(Keyascii As Integer)

If Keyascii > 0 And Keyascii < 48 _
Or Keyascii > 57 And Keyascii < 65 _
Or Keyascii > 90 And Keyascii < 97 _
Or Keyascii > 122 And Keyascii < 128 Then
      
    Keyascii = 0
    
    Exit Function
End If
End Function

Happy coding...

Jemz,
You can also use this which is lot simpler.

Private Sub txtmchno_KeyPress(KeyAscii As Integer)
Dim cKey As String

    cKey = Chr$(KeyAscii)
    If Not ((cKey >= "a" And cKey <= "z") Or (cKey >= "A" And cKey <= "Z") Or (cKey >= "0" And cKey <= "9")) Then
        ' Cancel the input character
        KeyAscii = 0
        'Message box to display
    End If
End Sub

Hope this helps

Well you message box says only to input alpha or numeric (0-9, a-z, A-Z) and you are allowing a whole lot more characters than that in your code.

And while I don't normally post in threads that I think have the proper answer and should soon be marked as solved but I could not resist in not only pointing the above out, but in also giving a little lesson in readability (Jemz, Andre... Although KingWang, while yours is easier to read, the conversion from integer to string and then comparing strings takes more processing power and thus is just a bit slower than comparing numbers...)

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then 'numbers 0-9
ElseIf KeyAscii >= vbKeyA And KeyAscii <= vbKeyZ Then 'uppercase A-Z
ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then 'lower case a-z
ElseIf KeyAscii = vbKeyBack Then 'just in case user makes a mistak
Else
  KeyAscii = 0
End If
End Sub

PS. There is a good reason VB has all those constants... because it makes the code easier to read...

Good Luck

hello sir thank you for the reply...i will try this sir...more power to you..

hello sir thank you for the reply i will try this sir and i will write again if i have doubt in my code...thank you sir.more power to you..

hello sir thank you for the reply ..yes it will accept only alpha and numeric..those ?.,#$@!*&^%+_){}|":?>< and etc.. are not be accepted...i will write again sir if i have doubt in my code sir...thank you for the reply sir....more power to you.

hello sir good morning..sir i tried your code and it works...sir thank you so much for helping me..by the way sir about the kinwang2009 what is ckey?....sir thank you for explaining me about your code it really big helps to me..hoping for your positive responds..more power to you sir...

hello sir, good morning i tried your code and it works thank you for helping me sir..

hello sir Good Morning, I tried your code and it works and thank you also for giving good advice in what to do with my program.more power to you sir....

cKey is a string variable declared in the sub there jemz, and it holds the string character that is the result of the chr functions conversion of the keyascii value. Then it is used in the if statement for string comparison to see if the character is acceptable or not...

Good Luck

hello sir thank you for the reply sir and helping me..i got it sir..thank you also to the people who help me in this thread...more power to you...

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.