Stuart McCall's Microsoft Access Pages - Keyboard

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

Get/Set the keyboard delay and speed

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
  (ByVal uAction As Long, ByVal uParam As Long, lpvParam As Any, ByVal fuWinIni As Long) As Long

Private Const SPIF_SENDCHANGE = 1
Private Const SPI_GETKEYBOARDSPEED = 10
Private Const SPI_SETKEYBOARDSPEED = 11
Private Const SPI_GETKEYBOARDDELAY = 22
Private Const SPI_SETKEYBOARDDELAY = 23
Paste these Declarations into the top of a standard module (ie before any procedures).
Public Function GetKBspeed(Speed As Long) As Boolean
'Pass a Long variable and this function
'will fill it with the current keyboard speed
'The Function returns True For success, False otherwise
   Dim Retcode As Long
   Dim tmp As Long
   
    Retcode = SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0&, tmp, 0&)
    Speed = tmp
    GetKBspeed = (Retcode > 0)
End Function

Public Function GetKBdelay(Delay As Long) As Boolean
'Pass a Long variable and this function
'will fill it with the current keyboard delay
'The Function returns True For success, False otherwise
   Dim Retcode As Long
   Dim tmp As Long
  
    Retcode = SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0&, tmp, 0&)
    Delay = tmp
    GetKBdelay = (Retcode > 0)
End Function

Public Function SetKBspeed(NewSpeed As Long) As Boolean
'Pass A Number Between 0 And 31
'31 Is The Fastest Speed
'0 Is The Slowest
'The Function returns True For success, False otherwise

   Dim Retcode As Long
   Dim dummy As Long

   Retcode = SystemParametersInfo(SPI_SETKEYBOARDSPEED, _
      NewSpeed, dummy, SPIF_SENDCHANGE)
 
  SetKBspeed = (Retcode > 0)
End Function

Public Function SetKBdelay(NewDelay As Long) As Boolean
'Pass A Value Beteen 0 And 3.  0 For The Shortest Delay
'(About 250 Ms), 3 For The Longest (About 1 Sec)
'The Function returns True For success, False otherwise

   Dim Retcode As Long
   Dim dummy As Long
    
     Retcode = SystemParametersInfo(SPI_SETKEYBOARDDELAY, _
         NewDelay, dummy, SPIF_SENDCHANGE)

      SetKBdelay = (Retcode > 0)
End Function
Paste this Code into the same module.
Usage:
Dim Speed As Long

If GetKBspeed(Speed) Then
    Debug.Print Speed
Else
    Debug.Print "Call to GetKBspeed failed."
End If

SetKBspeed 31  'Set to fastest speed
Home Contents