Quickbooks-style date increments/decrements?

David Spigelman
David Spigelman used Ask the Experts™
on
I'd like to setup a date field in an Access form, that can be incremented or decremented by pressing the + or - key, respectively, the way it works in Quickbooks. I thought that I could capture the keypress before saving the field, and do the job that way, by using code like this in the OnKeypress event:


Private Sub RequestDate_KeyPress(KeyAscii As Integer)
   Select Case KeyAscii
      Case 43
         RequestDate = RequestDate + 1
      Case 45
         RequestDate = RequestDate - 1
   End Select
End Sub

This seems to work, except that it still types a "+" or a "-" into the box. In fact, if the field value is highlighted, it erases the existing value to do it, although if I hit Esc afterwards, I can see that  the new, correct value, is in place.

How do I get it to just update the field with the new date value, and not insert a + or - into it?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Architect / Application Developer
Top Expert 2007
Commented:
Try this:

Private Sub RequestDate_KeyDown(KeyCode As Integer, Shift As Integer)
   
   Select Case KeyCode
      Case 187
         KeyCode = 0
         RequestDate = CDate(RequestDate) + 1
      Case 189
         KeyCode = 0
         RequestDate = CDate(RequestDate - 1)
   End Select

End Sub

mx
David SpigelmanPresident / CEO

Author

Commented:
That did it, mx. Actually, all that I was missing was setting KeyAscii = 0. That worked perfectly. Just curious, 187 and 189 seem to be drawing characters. Why were you using those?

Anyway, thanks a bunch!
David SpigelmanPresident / CEO

Author

Commented:
Thanks again!
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
"Why were you using those?"
Well, I thought ... those were the keycodes for plus and minus ... and they do work ... but, the keycodes are actually 43 and 45.  I quickly used Debug.Print Keycode ... and those were returned.

Not sure what is going on ...

mx
David SpigelmanPresident / CEO

Author

Commented:
Ok - that's kind of what I figured, but I thought I would ask. I could have been missing something.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
Actually, not sure WHAT ... is going on with those code.  Forget KeyDown ... what you came up with is correct ... just missing KeyAscii = 0

Private Sub RequestDate_KeyPress(KeyAscii As Integer)
   Debug.Print KeyAscii
   Select Case KeyAscii
      Case 43
         KeyAscii = 0
         RequestDate = CDate(RequestDate) + 1
      Case 45
         KeyAscii = 0
         RequestDate = CDate(RequestDate - 1)
   End Select
End Sub

mx
David SpigelmanPresident / CEO

Author

Commented:
Lol - Well, it works. Now - any thoughts on how to turn it into a general procedure or function, so I can use it all over the app? Getting a little confused over what I'd need to pass,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial