VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Filters out key presses. You can choose for lower/upper case letters, numbers, punctuation and comb

by VB-Kung-Fu (19 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sun 9th November 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Filters out key presses. You can choose for lower/upper case letters, numbers, punctuation and combinations of these. Useful for data entry

API Declarations


fltUpper
fltLower
fltNumber
fltPunctuation
fltUpperLower
fltUpperNumber
fltUpperPunctuation
fltUpperNumberPunctuation
fltLowerNumber
fltLowerPunctuation
fltLowerNumberPunctuation
fltNumberPunctuation
End Enum

Rate Filters out key presses. You can choose for lower/upper case letters, numbers, punctuation and comb



'   Private Sub Text1_KeyPress(KeyAscii As Integer)
'       'allows only numbers and uppercase letters
'       KeyAscii = AllowKeys(KeyAscii,fltUpperNumber)
'   End Sub

Public Function AllowKeys(KeyAscii As Integer, KeysToAllow As FilterConstants, Optional ShowErrMsg As Boolean) As Integer
'Note: all the filters allow the space(32) and back space keys

Dim Msg As String

Select Case KeysToAllow
  Case fltLower 'allow lowercase letters only
        If KeyAscii = vbKeyBack Or KeyAscii = vbKeySpace Then
          AllowKeys = KeyAscii
        ElseIf KeyAscii < 97 Or KeyAscii > 122 Then
          AllowKeys = 0
          Msg = "Only lower case letters are allowed. (E.g. 'a' but not 'A')"
        Else
          AllowKeys = KeyAscii
        End If
        
  Case fltUpper 'allow uppercase letters only
        If KeyAscii = vbKeyBack Or KeyAscii = vbKeySpace Then
          AllowKeys = KeyAscii
        ElseIf KeyAscii < 65 Or KeyAscii > 90 Then
          AllowKeys = 0
          Msg = "Only upper case letters are allowed. (E.g. 'A' but not 'a')"
        Else
          AllowKeys = KeyAscii
        End If
        
  Case fltNumber 'allow numbers only.The 46 is for the decimal point
        If KeyAscii = vbKeyBack Or KeyAscii = vbKeySpace Or KeyAscii = 46 Then
          AllowKeys = KeyAscii
        ElseIf KeyAscii < 48 Or KeyAscii > 57 Then
          AllowKeys = 0
          Msg = "Only digits are allowed. (E.g. '0','3' or '9')"
        Else
          AllowKeys = KeyAscii
        End If
        
  Case fltPunctuation 'allow punctuation keys only
        Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 33 To 47, 58 To 64, 91 To 96, 123 To 126
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only punctuatuon characters are allowed. (E.g. '?', ';' or ',')"
        End Select
        
   Case fltUpperLower  'allow upper and lowercase letters only
        Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 65 To 90, 97 To 122
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only lower case and upper case letters are allowed. (E.g. 'a','b','A' or 'B')"
        End Select
        
   Case fltUpperNumber 'allow uppercase letters and numbers only
        Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 65 To 90, 48 To 57
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only upper case letters and digits are allowed. (E.g. '0','5','A' or 'B')"
        End Select
        
  Case fltUpperPunctuation 'allow uppercase letters and punctuation only
        Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 65 To 90, 33 To 47, 58 To 64, 91 To 96, 123 To 126
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only upper case letters and punctuation characters are allowed. (E.g. '?',';','A' or 'B')"
        End Select
        
 Case fltLowerNumber 'allow lowercase letters and numbers only
        Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 97 To 122, 48 To 57
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only lower case letters and digits are allowed. (E.g. '0','5','a' or 'b')"
        End Select
        
 Case fltLowerPunctuation 'allow lowercase letters and punctuation only
       Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 97 To 122, 33 To 47, 58 To 64, 91 To 96, 123 To 126
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only lower case letters and punctuation characters are allowed. (E.g. '?',';','a' or 'b')"
        End Select
        
 Case fltNumberPunctuation 'allow numbers and punctuation only
       Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 48 To 57, 33 To 47, 58 To 64, 91 To 96, 123 To 126
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only digits and punctuation characters are allowed. (E.g. '?',';','6' or '2')"
        End Select
        
  Case fltUpperNumberPunctuation 'allow uppercase,numbers and punctuation only
         Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 65 To 90, 48 To 57, 33 To 47, 58 To 64, 91 To 96, 123 To 126
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only upper case letters, digits and punctuation characters are allowed. (E.g. '?','A' or '9')"
        End Select
        
   Case fltLowerNumberPunctuation 'allow lowercase,numbers and punctuation only
         Select Case KeyAscii
           Case vbKeyBack
             AllowKeys = KeyAscii
           Case vbKeySpace
             AllowKeys = KeyAscii
           Case 48 To 57, 97 To 122, 33 To 47, 58 To 64, 91 To 96, 123 To 126
             AllowKeys = KeyAscii
           Case Else
             AllowKeys = 0
             Msg = "Only lower case letters, digits and punctuation characters are allowed. (E.g. '?','a' or '9')"
        End Select
 End Select
 
 If AllowKeys = 0 And ShowErrMsg = True Then
    MsgBox Msg, vbInformation + vbOKOnly, "Invalid key"
 End If
End Function


Download this snippet    Add to My Saved Code

Filters out key presses. You can choose for lower/upper case letters, numbers, punctuation and comb Comments

No comments have been posted about Filters out key presses. You can choose for lower/upper case letters, numbers, punctuation and comb. Why not be the first to post a comment about Filters out key presses. You can choose for lower/upper case letters, numbers, punctuation and comb.

Post your comment

Subject:
Message:
0/1000 characters