speakers_86
Registered User.
- Local time
- Today, 10:09
- Joined
- May 17, 2007
- Messages
- 1,919
If you want to limit the characters entered into a field, you can use the control's keypress event:
KeyAscii - the integer from the keypress event
ctl - a reference to the calling control
intLength - number of characters allowed in the control
intCharacterSet - and enumeration of predefined character sets
strCustomCharacterSet - a string of allowed characters that you provide instead of using the predefined list
You can debug the GetValidKeys output to see each predefined character set. This does nothing for formatting or validating, merely limiting keystrokes to the proper type. Please let me know if there are any errors or if more predefined sets should be added.
Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)
If Not ValidChr(KeyAscii, Me.Text0, 20, enumPhoneNumber) Then KeyAscii = 0
End Sub
KeyAscii - the integer from the keypress event
ctl - a reference to the calling control
intLength - number of characters allowed in the control
intCharacterSet - and enumeration of predefined character sets
strCustomCharacterSet - a string of allowed characters that you provide instead of using the predefined list
You can debug the GetValidKeys output to see each predefined character set. This does nothing for formatting or validating, merely limiting keystrokes to the proper type. Please let me know if there are any errors or if more predefined sets should be added.
Code:
Option Compare Database
Option Explicit
Public Enum enumCharSet
enumAny = 0
enumAlpha = 1
enumNumbers = 2
enumAlphaNum = 3
enumPhoneNumber = 4
enumDecimal = 5
enumSpecial = 6
enumParagraph = 7
End Enum
Public Function GetValidKeys(intX As enumCharSet) As String
On Error GoTo err
Select Case intX
Case 1
GetValidKeys = "abcdefghijklmnopqrstuvwxyz"
Case 2
GetValidKeys = "0123456789"
Case 3
GetValidKeys = "abcdefghijklmnopqrstuvwxyz0123456789"
Case 4
GetValidKeys = "()-0123456789"
Case 5
GetValidKeys = ".0123456789"
Case 6
GetValidKeys = "!@#$%^&*() -_=+[]\{}|;:',./?><`~" & Chr(34)
Case 7
GetValidKeys = " abcdefghijklmnopqrstuvwxyz.?!;,"
Case Else
Debug.Print "Unhandled case in GetValidKeys"
GetValidKeys = ""
End Select
Exit Function
err:
Debug.Print err.Description
End Function
Public Function ValidChr(KeyAscii As Integer, ctl As Control, Optional intLength As Integer = -1, Optional intCharSet As enumCharSet, Optional strCustomCharacterSet As String) As Boolean
On Error GoTo err
Dim strAllowed As String
'backspace and tab
Select Case KeyAscii
Case 8, 9
ValidChr = True
Exit Function
End Select
'determine the authorized characters
If Len(strCustomCharacterSet) > 0 Then
strAllowed = strCustomCharacterSet
ElseIf intCharSet > 0 Then
strAllowed = GetValidKeys(intCharSet)
End If
'length is set to 0, so no characters are authorized
If intLength = 0 Then
ValidChr = False
Exit Function
End If
'limit the chacacter length to intLength
If intLength > 0 Then
If Len(Nz(ctl.Text, 0)) >= intLength Then
ValidChr = False
Exit Function
End If
End If
'allowed keys are not defined, so allow any
If Len(strAllowed) = 0 Then
ValidChr = True
Exit Function
End If
'check if the typed key is in the allowed list
If Nz(InStr(1, strAllowed, Chr(KeyAscii)), 0) = 0 Then
ValidChr = False
Exit Function
End If
'no rules were violated, so allow the press
ValidChr = True
Exit Function
err:
Debug.Print err.Description
End Function