Arrow keys to next / previous record

Gkirkup

Registered User.
Local time
Today, 01:07
Joined
Mar 6, 2007
Messages
628
I have an application where records are displayed one at a time, and the user clicks an 'arrow' button on screen to display the next or previous record.
I would like to add the capability to use the keyboard up/down arrow keys to do the same thing. So in the KeyDown property of a control I used:
DoCmd.GoToRecord, , acnext
That worked fine. Then in the KeyUp property I used:
DoCmd.GoToRecord, ,acPrevious
That didn't work as expected, but gave me the next record, not the previous one.
How do I get to the previous record with the up arrow key?

Robert
 
KeyUP means when the key goes up (not key up as in moving records).

KeyDown means when your key physically moves in the downward motion.

So you would need to capture which keystroke is being sent (40 for down and 38 for up)
 
SOS, thanks. How would I capture the keystroke being sent?

Robert
 
Try the KeyUp event of the form (remember to set the KeyPreview property of the form to YES) and use:

Code:
Select Case KeyCode
 
   Case 38
       DoCmd.GoToRecord, , acnext
   Case 40
       DoCmd.GoToRecord, , acprevious
End Select
 
SOS: Thanks, that worked great, except that I think the key codes were reversed - 38 is up arrow and 40 is down arrow. Now my problem is that when I move to the next or previous record this way, the focus doesn't stay on the same control, but the next one to the right, as if you also tabbed. That makes it look odd. Any easy fix for that?

Robert
 
SOS: Thanks, that worked great, except that I think the key codes were reversed - 38 is up arrow and 40 is down arrow. Now my problem is that when I move to the next or previous record this way, the focus doesn't stay on the same control, but the next one to the right, as if you also tabbed. That makes it look odd. Any easy fix for that?

Robert

How about adding the

Me.YourControlYouWantTheFocusOn.SetFocus

???
 
SOS: Yes, I could do that. At present the code is in the form, not individual controls. I would have to move the KeyCode capture to each individual control and then do a set focus to that control, to make this work. Not a bid deal.

Robert
 
Just make a single function and then call it from that function.

Code:
Function SetFoc(frm As Form, ctl As Control)
   frm.ctl.SetFocus
End Function
And then you can call it from the Key event like this:

Code:
Call SetFoc(Me, Screen.ActiveControl)

"Air Code" (untested) but it should work.
 
I found this and understand the code, but where do I add it to my form. Also, how do I set the form so that when the user is on the last record in the form, the down arrow will scroll the user to the first record?
 

Users who are viewing this thread

Back
Top Bottom