navigating within a date field
-
2012年4月14日 15:26
I have a date field in a form. I'd like to be able to use keystrokes to navigate within that field, e.i., key down will move the date forward by one day, key up will move back the date by one day. (Ideally, I'd rather use right key/left key, but I can't find any way to do this).
I used the following code:
Private Sub DateofService_KeyDown(KeyCode As Integer, Shift As Integer)
Me.DateofService = DateofService.Value + 1
Me.DateofService.SelStart = Me.DateofService.SelLength
End SubPrivate Sub DateofService_KeyUp(KeyCode As Integer, Shift As Integer)
Me.DateofService = DateofService.Value - 1
Me.DateofService.SelStart = Me.DateofService.SelLength
End SubHowever the two seem to conflict with each other— if I put in just one code or the other, it will work on either key (in fact it works on any of the arrow keys), but if I put in both pieces of code, the Value +1 code overrides the Value -1 code.
I want to emphasize that this is just a little nicety— not worth hours of figuring out. Still, I thought maybe someone's already dealt with this one....? Are there keywords to use within the code to distinguish the different diorections?
THanks!
—nick
PS: I just discovered that "enter" and "tab" trigger this code as well -- definitiely not the desired effect!- 已编辑 Nick Vittum 2012年4月14日 15:28
全部回复
-
2012年4月14日 15:43
I discovered after I posted this that any keystroke at all activates the code, making it impossible to enter a date using the number keys. I eliminated the code. But if anyone has an suggestions about how my purpose might be achieved, I'd love to hear.
—nick
-
2012年4月14日 15:57
KeyDown and KeyUp do not refer to the down arrow and up arrow keys, but to the actions of depressing a key (any key) and releasing it again.
The KeyCode argument tells you which key was depressed/released, and the Shift argument tells you whether the modifier keys Shift, Alt and/or Ctrl were held down.
Remove the On Key Up code, and change the On Key Down code to
Private Sub DateOfService_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyLeft Me.DateOfService = CDate(Me.DateOfService.Text) - 1 Me.DateOfService.SelStart = Me.DateOfService.SelLength KeyCode = 0 Case vbKeyRight Me.DateOfService = CDate(Me.DateOfService.Text) + 1 Me.DateOfService.SelStart = Me.DateOfService.SelLength KeyCode = 0 End Select End Sub
Setting KeyCode to 0 cancels the original key action.Regards, Hans Vogelaar
- 已标记为答案 Nick Vittum 2012年4月14日 16:10
-
2012年4月14日 16:22
Wonderful, Hans, thank you very much!
- In the code above, what does "CDate" mean?
- And what does "Case" refer to? It doesn't seem to have anything to do with upper or lower case?
- Regarding KeyCode arguments: do you know where I can find more detail? the names of various keys and more on the syntax for using them? I can imagine this knowledge must be very useful. A quick Google search for "Access: KeyCode arguments" revealed on fragmentary information, but it seems there must be a resource on this.
Thanks once again
—nick
-
2012年4月14日 16:47
I have a date field in a form. I'd like to be able to use keystrokes to navigate within that field, e.i., key down will move the date forward by one day, key up will move back the date by one day. (Ideally, I'd rather use right key/left key, but I can't find any way to do this).
Hi Nick,
Hans gave you already the answer. I have some additional remarks.
I made a kind of a method of this possibility: as soon as I define a date control, it has the possibily to increase or decrease its value by this method. So it works on any date control in any application.
However, I do not use the Left or Right arrow key, because I have reserved these for movement between the controls. Instead I use the "lower case '<' of '>' keys' (that is just the comma or point) to change the date. That gives also the possibility to use the "upper case '<' or '>' keys" (that is the real < or >) for a decrease or increase per month.
Instead of Hans' increase statement:
Me.DateOfService = CDate(Me.DateOfService.Text) + 1
I just use
Me.DateOfService = Me.DateOfService + 1
Imb.
-
2012年4月14日 16:54
1. CDate converts a text string to a date value. The text in the text box is a string, you can't just add 1 to it. You must convert the string to a date first, then add 1.
2. Select Case ... End Case is a different way of writing a series of If ... Else If ... End If. See for example http://www.techonthenet.com/excel/formulas/case.php (it's written for Excel but applies to Access too)
3. See http://msdn.microsoft.com/en-us/library/ff844722.aspx for general info about the On Key Down event.
See http://msdn.microsoft.com/en-us/library/aa243025(v=vs.60).aspx for a list of keycode constants.
Regards, Hans Vogelaar
-
2012年4月14日 17:07
Thank you, Hans. I had seen the Select Case/End Case in other code and wondered what it meant. ANd thank you for these resources. I ill bookmark them and read up. Seems like very useful stuff.
Be well!
—nick
-
2012年4月14日 17:16
Thanks, IMB
I'm intrigued. What do you mean by a method? Have you created a module that somehow automatically "speaks" to any field with this data type?
—nick

