Rolling dates by pressing "+" or "-"

        4 votes: *****     2,685 views      No comments
by Allen Browne, 20 April 2005    (for Access v2+)

Microsoft Access Tips for Serious Users

Provided by Allen Browne, allenbrowne.com


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

Rate this article:  Your rating: PoorYour rating: Not so goodYour rating: AverageYour rating: GoodYour rating: Excellent


This is a cached tutorial, reproduced with permission.

Have your say - comment on this article.

What did you think of 'Rolling dates by pressing "+" or "-"'?

No comments yet.

Why not be the first to comment on this article?!

Have your say...

Name
E-mail (e-mail address will be kept private)
Comments


Comments require approval before being displayed on this page (allow 24 hours).