Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    KeyPreview - Shift, Alt, and Ctrl (97)

    Hello

    I use the KeyPress event of a form to modify the contents of an unbound textbox that contains a Date/Time value. Currently, I use +/- to modify the date and Shift +/- to modify the hour, but would like to extend the functionality into modifying the minutes. My first inclination was to use Ctrl +/- or Alt +/-, but both of these combinations appear to have dedicated meanings within Access. Ctrl appears to execute some record-navigation functions, and Alt launches the menu.

    Is there any way to disable these system-level functions so I could hook into either one of them?

    Here is the code that I use for the KeyPress:

    Private Sub txtDeliveryTime_KeyPress(KeyAscii As Integer)
    KeyAscii = DateHandler(KeyAscii, Me.ActiveControl)
    End Sub

    Private Declare Function GetAsyncKeyState Lib "user32" _
    (ByVal vKey As Long) As Integer

    Public Function DateHandler(KeyAscii As Integer, ctl As Control) As Integer
    Dim sUnit As String
    Dim nRet As Integer
    ' This routine adds or subtracts days, based on the
    ' key pressed, from a date value found in the control
    ' represented by the form's ActiveControl property
    ' (usually a TextBox). The routine can be altered to
    ' add and subtract months and years too.
    On Error GoTo errorHandler
    ' Constants which represent the '+' & '-' keys.
    Const KeyAdd = 43
    Const KeySubtract = 45
    ' This constant is here because '+' & '=' are on the
    ' same key for most keyboards, but are sometimes inverted.
    Const KeyEquals = 61

    sUnit = "d" ' default to DAY arithmetic
    If GetAsyncKeyState(vbKeyShift) Then
    sUnit = "h"
    End If


    ' Determine the value of the key pressed, and
    ' take the necessary action.
    Select Case KeyAscii
    Case KeyAdd, KeyEquals
    ctl.Text = DateAdd(sUnit, 1, ctl)
    nRet = 0
    Case KeySubtract
    ctl.Text = DateAdd(sUnit, -1, ctl)
    nRet = 0
    Case Else
    nRet = KeyAscii
    End Select
    ' Move the start position to the end of the
    ' text for a cleaner look.
    If nRet = 0 Then
    ctl.SelStart = Len(ctl.Text)
    End If

    ' Return a new KeyAscii value.
    DateHandler = nRet
    Exit Function

    errorHandler:
    DateHandler = 0
    Exit Function
    End Function
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: KeyPreview - Shift, Alt, and Ctrl (97)

    Don't try it. The Alt key behavior is Windows, not Access, and I highly recommend that you avoid tampering with system behaviors. If the focus in on the minutes and seconds, the existing behavior should be adaptable to the time.

    Frankly, I've disabled all the equivalent behaviors in our applications because they caused us problems when we migrated to 2002 and we found that the users weren't using them anyhow.
    Charlotte

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: KeyPreview - Shift, Alt, and Ctrl (97)

    Apart from Charlotte's comments, you can use the On Key Down event instead of the On Key Press event. This has two advantages:
    - It has a Shift argument that passes the state of the modifier keys, so you don't need a Windows API function.
    - It fires before the default Access action occurs, so you can trap Ctrl+- for example.

    Here is a modified function:

    <code>Public Function DateHandler(KeyCode As Integer, Shift As Integer, ctl As Control) As Integer</code>
    <code> Dim sUnit As String</code>
    <code> Dim nRet As Integer</code>
    <code> ' This routine adds or subtracts days, based on the</code>
    <code> ' key pressed, from a date value found in the control</code>
    <code> ' represented by the form's ActiveControl property</code>
    <code> ' (usually a TextBox). The routine can be altered to</code>
    <code> ' add and subtract months and years too.</code>
    <code> On Error GoTo errorHandler</code>
    <code> ' Constants which represent the '+' & '-' keys.</code>
    <code> Const KeyAdd = 43</code>
    <code> Const KeySubtract = 189</code>
    <code> ' This constant is here because '+' & '=' are on the</code>
    <code> ' same key for most keyboards, but are sometimes inverted.</code>
    <code> Const KeyEquals = 187</code>

    <code> sUnit = "d" ' default to DAY arithmetic</code>
    <code> If Shift And acShiftMask Then</code>
    <code> sUnit = "h"</code>
    <code> End If</code>
    <code> If Shift And acCtrlMask Then</code>
    <code> sUnit = "n"</code>
    <code> End If</code>

    <code> ' Determine the value of the key pressed, and</code>
    <code> ' take the necessary action.</code>
    <code> Select Case KeyCode</code>
    <code> Case vbKeyAdd, KeyEquals</code>
    <code> ctl.Text = DateAdd(sUnit, 1, ctl)</code>
    <code> nRet = 0</code>
    <code> Case vbKeySubtract, KeySubtract</code>
    <code> ctl.Text = DateAdd(sUnit, -1, ctl)</code>
    <code> nRet = 0</code>
    <code> Case Else</code>
    <code> nRet = KeyCode</code>
    <code> End Select</code>
    <code> ' Move the start position to the end of the</code>
    <code> ' text for a cleaner look.</code>
    <code> If nRet = 0 Then</code>
    <code> ctl.SelStart = Len(ctl.Text)</code>
    <code> End If</code>

    <code> ' Return a new KeyAscii value.</code>
    <code> DateHandler = nRet</code>
    <code> Exit Function</code>

    <code>errorHandler:</code>
    <code> DateHandler = 0</code>
    <code>End Function</code>

    Note that it has an extra argument Shift. Call like this: (corrected)

    <code>Private Sub txtDeliveryTime_KeyDown(KeyCode As Integer, Shift as Integer)</code>
    <code> KeyAscii = DateHandler(KeyCode, Shift, Me.ActiveControl)</code>
    <code>End Sub</code>

  4. #4
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: KeyPreview - Shift, Alt, and Ctrl (97)

    Works like a charm! Thanks very much.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •