Microsoft Access Tips for Serious Users

Provided by Allen Browne

Rolling dates by pressing "+" or "-"

Some commercial programs (Tracker, Quicken, etc) allow the user to press "+" or "-" to increment or decrement a date without the hassle of selecting the day part of the field and entering a new value. This is especially useful in fields where the default date offered could be a day or two different from the date desired.

To provide this functionality in Access, attach this Event Procedure to the KeyPress event of your control.

Select Case KeyAscii
    Case 43            ' Plus key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl + 1
    Case 45            ' Minus key
        KeyAscii = 0
        Screen.ActiveControl = Screen.ActiveControl - 1
    End Select

When any alphanumeric key is pressed, Access passes its ASCII value to your event procedure in the variable KeyAscii. The code examines this value, and acts only if the value is 43 (plus) or 45 (minus). It destroys the keystroke (so it is not displayed) by setting the value of KeyAscii to zero. The active control is then incremented or decremented.

This idea is not limited to dates, or even textboxes. The code can be adapted for other keystrokes as required. Use the KeyDown event to distinguish between the two plus keys (top row and numeric keypad), or to trap control keys such as {Esc}. Anyone feel like reprogramming an entire keyboard?

Home Index of tips Top