KeyPress event

  • Thread starter Thread starter grep
  • Start date Start date
G

grep

I had an interesting idea regarding a date field. I want to do a
Quicken-style date change thing... where you can press + or - to change
the date in the field. I figured it would be easy, using the KeyPress
event, but it's not working the way I thought. Here's what I tried:

Private Sub Date_KeyPress(KeyAscii As Integer)
Dim CurrentDate As Date

CurrentDate = Me.Date

Select Case KeyAscii
Case 45 ' ANSI value of -
Me.Date = DateAdd("d", -1, CurrentDate)
Case 43 ' ANSI value of +
Me.Date = DateAdd("d", 1, CurrentDate)
End Select
End Sub

Any thoughts?

grep
 
FYI, I figured out how to do it.

I noticed that the date value was actually incrementing, but that the
string "-" or "+" were being left in the field (which was illegal). When
I hit Esc, however, the date did increment. That led me to the following:

Private Sub Date_KeyPress(KeyAscii As Integer)
Dim CurrentDate As Date

CurrentDate = Me.Date

Select Case KeyAscii
Case 45
Me.Date = DateAdd("d", -1, CurrentDate)
Case 43
Me.Date = DateAdd("d", 1, CurrentDate)
End Select

SendKeys "{ESC}", True

End Sub

It works beautifully now.

grep
 
Ok - I still need help. I tried to put it into a general module so I
could use it across many forms, and it's no longer working right.
Specifically, it seems that the Sendkeys command throws it into a Stack
Overflow.

Here's what it look like now:

Public Sub DatePush(MyKey As Integer)
' Increments date field using "+" and "-"
Dim CurrentDate As Date
Dim frmCurrentForm As Form

Set frmCurrentForm = Screen.ActiveForm
CurrentDate = frmCurrentForm.Date

Select Case MyKey
Case 45
frmCurrentForm.Date = DateAdd("d", -1, CurrentDate)
Case 43
frmCurrentForm.Date = DateAdd("d", 1, CurrentDate)
End Select


SendKeys "{ESC}", True

End Sub

Any suggestions as to how to get around the problem?

grep
 
Back
Top