Rolling dates by pressing "+" or "-"

B

Byron

Using Windows Vista & Access 2007

The following is taken from: Allen Browne's tips for Microsoft Access

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

I've followed the instructions above, but I get an error at the "KeyAscii."
The Error Message says that the Variable KeyAscii not declared.
Does this indicate a missing reference, and if so which one; or is there
something else I'm missing?

Thanks for any and all assistance.
Byron
 
K

Ken Snell \(MVP\)

Allen's example assumes that you already have an event procedure in place,
and that you just need to add the steps he's posted to that existing code.

Post the entire VBA code that you have for the KeyPress event.
 
L

Linq Adams via AccessMonster.com

Either of these should work. Just replace YourDateField with the actual name
of your textbox. All it requires is that the cursor is in the textbox when
pressing the Plus/Minus key.

Private Sub YourDateField_KeyPress(KeyAscii As Integer)
If KeyAscii = 43 Then
KeyAscii = 0
Me.YourDateField = Me.YourDateField + 1
ElseIf KeyAscii = 45 Then
KeyAscii = 0
Me.YourDateField = Me.YourDateField - 1
End If
End Sub


Or, for those who don’t like the Date + 1 syntax


Private Sub ExpDate_KeyPress(KeyAscii As Integer)
If KeyAscii = 43 Then
KeyAscii = 0
Me.ExpDate = DateAdd("d", 1, Me.ExpDate)
ElseIf KeyAscii = 45 Then
KeyAscii = 0
Me.ExpDate = DateAdd("d", -1, Me.ExpDate)
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top