Full Version: Entering and incrementing the date on a form
UtterAccess Forums > Microsoft® Access > Access Forms
Forrestleigh
Hope I've got the right forum,
I have an access97 database, what I'd like to do on a form is when the date field recieves the focus I want to be able to press either the up or down cursor key on the keyboard and have the current date entered, then whenever the up arrow is pressed again the date to increment by one and the down arrow would reduce the date also one day at a time
Any help would be much appreciated
Thanks
bulsatar
for up arrow:
extbox1.Value = DateAdd("d",1,Textbox1.value)
for the down arrow:
Textbox1.Value = DateAdd("d",-1,Textbox1.Value)
Forrestleigh
Thank you, that will do the dates, but that won't identify that its the up arrow key or the down arrow key, I need the code that identifies the key press then relates that key press to the date box
bulsatar
I got it to work with this:
!--c1-->
CODE
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then Text2.Value = DateAdd("d", 1, Text2.Value)
If KeyCode = vbKeyDown Then Text2.Value = DateAdd("d", -1, Text2.Value)
End Sub

However, I can not get it to stop the default action of cycling through the controls on the page. So you hit the arrow, it changes the value and then skips to the next control... iconfused.gif
Might need someone else's help on that one.
Forrestleigh
Thank you for that, i get the same but its a start and something I can now work on
R#>/thumbup.gif" style="vertical-align:middle" emoid=":thumbup:" border="0" alt="thumbup.gif" />
bulsatar
As another option, you could add a couple of buttons to the side to change the date or even a spinner.
Forrestleigh
Yep - I've tried everything to get the focus back after it increments with no success, I think the button idea is a really good one - thanks, I think I'd just got hung up on using the up/down arrow I couldn't see any other solution.
missinglinq
The trick to re-assigning a key usually used by Access and preventing the default behavior is to set Keycode to Zero:
CODE
Private Sub DateField_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then
DateField.Value = DateAdd("d", 1, DateField.Value)
KeyCode = 0
End If
If KeyCode = vbKeyDown Then
DateField.Value = DateAdd("d", -1, DateField.Value)
KeyCode = 0
End If
End Sub

If course, if you go to Tools - Options - Keyboard and set Arrow Key Behavior to Next Character instead of Next Field, you won't have the problem!
Linq ;0)>
bulsatar
Thanks missinglinq!!!
missinglinq
Glad we could help!
Forrestleigh
Fantastic, many many thanks order="0" alt="thumbup.gif" />
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.