Go Back   Access World Forums > Microsoft Access Reference > Code Repository

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 02-05-2016, 03:29 PM   #1
speakers_86
I am jack's comment.
 
speakers_86's Avatar
 
Join Date: May 2007
Location: JBLM, Wa
Posts: 1,919
Thanks: 11
Thanked 158 Times in 119 Posts
speakers_86 will become famous soon enough
Limit Characters from KeyPress

If you want to limit the characters entered into a field, you can use the control's keypress event:

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

__________________
If you look, you can find anything.
Google is your friend.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


~~~~~~~~~~~~~~~~~~~~~~~~~~~
Access 2010 screw this! I went back to 2007
Windows 7
speakers_86 is offline   Reply With Quote
The Following 2 Users Say Thank You to speakers_86 For This Useful Post:
hassanogaibi (06-28-2016), tonez90 (05-28-2017)
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Limit field result to 30 Characters benkingery Queries 3 11-10-2011 07:14 AM
Need to limit field size between 14 and 30 characters justiwhi Forms 5 09-11-2008 05:24 AM
Limit the No. of characters in an unbound textbox? MsLady Forms 13 09-25-2006 09:26 AM
limit characters per field Neguez General 1 05-09-2006 08:23 PM
limit characters for form textbox? CarlyS Forms 5 10-22-2004 05:44 PM




All times are GMT -8. The time now is 12:53 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World