Stuart McCall's Microsoft Access Pages - Keyboard

  • Convert keypresses to uppercase as u type
  • Convert keypresses to lowercase as u type
  • Restrict keypresses to Y and N as u type
  • Restrict number of keypresses to no more than [n] as u type
  • Restrict keypresses to only numbers as u type
  • Restrict keypresses to only numbers and period (.) as u type
  • Filter keypresses to disallow certain characters as u type
  • Home
    Convert keypresses to uppercase as u type

    Public Function KeyUpper(KeyAscii As Integer)
    ' Called from keypress event of an unbound textbox
    ' Forces upper-case entry
    '
    ' Syntax: KeyUpper KeyAscii
    
        KeyAscii = Asc(UCase(Chr(KeyAscii)))
    End Function
    
    Paste this Code into a standard module.
    Home Contents

    Convert keypresses to lowercase as u type

    Public Function KeyLower(KeyAscii As Integer)
    ' Called from keypress event of a textbox
    ' Forces lower-case entry
    '
    ' Syntax: KeyLower KeyAscii
    
        KeyAscii = Asc(LCase(Chr(KeyAscii)))
    End Function
    
    Paste this Code into a standard module.
    Home Contents

    Restrict keypresses to Y and N as u type

    Requires: KeyUpper

    Public Function KeyYN(KeyAscii As Integer)
    ' Called from keypress event of a textbox
    ' Forces 'Y' or 'N' entry
    '
    ' Syntax: KeyYN KeyAscii
    
        KeyUpper KeyAscii
        If KeyAscii <> 89 Or KeyAscii <> 78 Then
            KeyAscii = 0
            Beep
        End If
    End Function
    
    Paste this Code into a standard module.
    Home Contents

    Restrict number of keypresses to no more than n as u type

    Public Function KeyLimit(KeyAscii As Integer, ByVal Text As String, ByVal limit As Integer)
    ' Called from keypress event of an unbound textbox
    ' Restricts the number of characters to limit passed
    '
    ' Syntax: KeyLimit KeyAscii, Me![Textbox].Text, 30
    
        Select Case KeyAscii
            Case 8, 9, 13, 27  'Backspace, Tab, Enter, Esc
                Exit Function
        End Select
        If (Len(Text) - Screen.ActiveControl.SelLength) > limit Then
            KeyAscii = 0
            Beep
        End If
    End Function
    
    Paste this Code into a standard module.
    Home Contents

    Restrict keypresses to only numbers as u type

    Public Function KeyNumeric(KeyAscii As Integer)
    ' Called from keypress event of an unbound textbox
    ' Restricts the keypresses to numeric chars only
    '
    ' Syntax: KeyNumeric KeyAscii
    
        Select Case KeyAscii
            Case 8, 9, 13, 27  'Backspace, Tab, Enter, Esc
                Exit Function
        End Select
        Select Case KeyAscii
            Case 48 To 57
                'Do nothing
            Case Else
                Beep
                KeyAscii = 0
        End Select
    End Function
    
    Paste this Code into a standard module.
    Home Contents

    Restrict keypresses to only numbers and period (.) as u type

    Public Function KeyDecimal(KeyAscii As Integer)
    ' Called from keypress event of an unbound textbox
    ' Restricts the keypresses to numeric chars and period only
    '
    ' Syntax: KeyDecimal KeyAscii
    
        Select Case KeyAscii
            Case 8, 9, 13, 27  'Backspace, Tab, Enter, Esc
                Exit Function
        End Select
        Select Case KeyAscii
            Case 48 To 57, 46
                'Do nothing
            Case Else
                Beep
                KeyAscii = 0
        End Select
    End Function
    
    Paste this Code into a standard module.
    Home Contents

    Filter keypresses to disallow certain characters as u type

    Public Function KeyFilter(KeyAscii As Integer, Excludes As String)
    ' Called from keypress event of a textbox
    ' Disallows all characters found in Excludes
    '
    ' Syntax: KeyFilter KeyAscii, "!£$%^&*()\/|?'#~@;:[]{}-_=+"
    
        Select Case KeyAscii
            Case 8, 9, 13, 27  'Backspace, Tab, Enter, Esc
                Exit Function
        End Select
        If InStr(1, Excludes, Chr$(KeyAscii)) Then
            Beep
            KeyAscii = 0
        End If
    End Function
    
    Paste this Code into a standard module.
    Home Contents