Detecting Up or Down arrow (1 Viewer)

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
Is it possible to detect the Up or Down arrow keys?

I've tried using the following in the Form's Key Press event;
Code:
MsgBox "You pressed the " & KeyAscii & " Key"
Which returns the ASCII code for most other key but not the arrow keys :confused:
 

JHB

Have been here a while
Local time
Today, 16:33
Joined
Jun 17, 2012
Messages
7,732
Use the event Key Down. KeyCode are 40 and 38, for Up or Down arrow keys.
 

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
Thanks for that it's got me a little closer, but.

Code:
    If keyascii = 38 Or 40 Then
        MsgBox "you pressed a key"
    End If

Works fine, but
Code:
    If keyascii = 38 Then
        MsgBox "you pressed a key"
    End If
or
Code:
    If keyascii = 40 Then
        MsgBox "you pressed a key"
    End If
both fail to detect the appropriate key :confused:
 

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
My bad :eek:
Code:
    If keyascii = 38 Or 40 Then
        MsgBox "you pressed a key"
    End If
is wrong and detects any key down, and the correct syntax of;
Code:
    If keyascii = 38 Or keyascii = 40 Then
        MsgBox "you pressed a key"
    End If
fails too :banghead:
 

ChrisO

Registered User.
Local time
Tomorrow, 00:33
Joined
Apr 30, 2003
Messages
3,202
The name of the argument passed to the Form_KeyDown event is KeyCode.

If you use keyascii then it will be a Variant with a value of 0.

Try adding Option Explicit to the code module.

Chris.
 

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
Yep that did it.

Just in case someone else is looking for a similar solution the code I have is as follows;
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
    
On Error GoTo ErrorHandler
    
[COLOR="DarkGreen"]    'Detect Down arrow[/COLOR]
    If KeyCode = 40 Then
        DoCmd.GoToRecord acDataForm, "YourFormName", acNext
    End If
    
[COLOR="DarkGreen"]    'Detect Up arrow[/COLOR]
    If KeyCode = 38 Then
        DoCmd.GoToRecord acDataForm, "YourFormName", acPrevious
    End If
    
ErrorHandler:
[COLOR="DarkGreen"]
'Detect and deal with reaching the end of the record set[/COLOR]
    If Err.Number = 2105 Then
        KeyCode = -1
        Exit Sub
    End If
    
End Sub
The code allows the user to navigate a continuous form via the up down arrow keys
 

RainLover

VIP From a land downunder
Local time
Tomorrow, 00:33
Joined
Jan 5, 2009
Messages
5,041
John

'Detect and deal with reaching the end of the record set
Can you expand on this. How does one reach the end of the Record Set. Or are you simply saying that you have gone past all the current records and are on a new blank record.

Thanks.
 

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
Runtime error 2105 "You can't go to the specified record", occurs if you are on the first record and try to move to the previous or are on a new record and try to got to the next record.
 

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
OK I know I've overlooked something very simple :banghead: but for the life of me I can't seem to formulate the correct code to make this work when it's transferred to a sub form.
 

John Big Booty

AWF VIP
Local time
Tomorrow, 00:33
Joined
Aug 29, 2005
Messages
8,263
This is what you need for a sub form;
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

On Error GoTo ErrorHandler
    
    If KeyCode = 40 Then
        Forms![MainFormName].SetFocus
        Forms![MainFormName]![SubFormName].SetFocus
        DoCmd.GoToRecord , , acNext
    End If
    
    If KeyCode = 38 Then
        Forms![MainFormName].SetFocus
        Forms![MainFormName]![SubFormName].SetFocus
        DoCmd.GoToRecord , , acPrevious
    End If
    
ErrorHandler:
    If Err.Number = 2105 Then
        KeyCode = -1
        Exit Sub
    Else
        MsgBox Err.Description
        Resume Exit_Form_KeyDown
    End If


End Sub
 
Last edited:

RainLover

VIP From a land downunder
Local time
Tomorrow, 00:33
Joined
Jan 5, 2009
Messages
5,041
John

For what it is worth you don't have a proper error handler.

Other than " If Err.Number = 2105 Then"
 

Users who are viewing this thread

Top Bottom