working with keystrokes

S

Sophie

I have a textbox that contains a date. When that control has the focus, I'd
like to be ably to key in 'y' to move the date ahead by a year, and key in
'r' to move the date back by a year. How would I do this?
 
D

Dirk Goldgar

Sophie said:
I have a textbox that contains a date. When that control has the focus,
I'd
like to be ably to key in 'y' to move the date ahead by a year, and key in
'r' to move the date back by a year. How would I do this?


You could use code in the text box's KeyPress event. Something like this:

'----- start of code -----
Private Sub DateField_KeyPress(KeyAscii As Integer)

With Me!DateField

If Not IsNull(.Value) Then

Select Case KeyAscii

Case 121, 89 ' "y" or "Y"
.Value = DateAdd("yyyy", 1, .Value)
KeyAscii = 0 ' swallow this key

Case 114, 82 ' "r" or "R"
.Value = DateAdd("yyyy", -1, .Value)
KeyAscii = 0 ' swallow this key

End Select

End If

End With

End Sub
'----- end of code -----
 

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

Similar Threads


Top