There are several things to consider to get that to work as expected.
Firstly, if the record is dirty, it must be saved before you move record.
Several things could go wrong there, e.g. validation rule not met, required
field not filled in, or duplicate index violation.
Next, you may already be at the first record (trying to go up), or at the
new record (so cannot go further.)
There is also the possibility that the cursor is in a multi-line control
(typically a text box that has a vertical scrollbar, or has EnterKeyBehavior
set to New Line in Field), where the down or up arrow should probably move
down or up in the text box.
Next, there is the possiblity that the focus is in a control in the form
header or form footer section, where moving up or down does not make sense.
The code below takes care of all those cases. To use it in any Continuous
form, just set the form's KeyPreview property to Yes, and in the form's
KeyDown event procedure:
Call ContinuousUpDown(Me, KeyCode)
Replace the call to LogError() with your own error handler, or you can grab
the one from:
http://allenbrowne.com/ser-23a.html
The one thing this code does not handle is if the focus is in a combo box
that is dropped down, you might expect up/down to move up/down the choices
in the combo's list instead of moving record.
---------------------code starts----------------------
Public Sub ContinuousUpDown(frm As Form, KeyCode As Integer)
On Error GoTo Err_ContinuousUpDown
'Purpose: Respond to Up/Down in continuous form, by moving record,
' unless the active control's EnterKeyBehavior is on.
'Usage: Call ContinuousUpDown(Me, KeyCode)
Dim sForm As String
sForm = frm.Name
Select Case KeyCode
Case vbKeyUp
If ContinuousUpDownOk Then
'Save any edits
If frm.Dirty Then
RunCommand acCmdSaveRecord
End If
'Go previous: error if already there.
RunCommand acCmdRecordsGoToPrevious
KeyCode = 0 'Destroy the keystroke
End If
Case vbKeyDown
If ContinuousUpDownOk Then
'Save any edits
If frm.Dirty Then
frm.Dirty = False
End If
'Go to the next record, unless at a new record.
If Not frm.NewRecord Then
RunCommand acCmdRecordsGoToNext
End If
KeyCode = 0 'Destroy the keystroke
End If
End Select
Exit_ContinuousUpDown:
Exit Sub
Err_ContinuousUpDown:
Select Case Err.Number
Case 2046, 2101, 2113, 3022, 2465 'Already at first record, or save
failed, or The value you entered isn't valid for this field.
KeyCode = 0
Case Else
Call LogError(Err.Number, Err.Description, "ContinuousUpDown()",
"Form = " & sForm)
End Select
Resume Exit_ContinuousUpDown
End Sub
Private Function ContinuousUpDownOk() As Boolean
On Error GoTo Err_ContinuousUpDownOk
'Purpose: Suppress moving up/down a record in a continuous form if:
' - control is not in the Detail section, or
' - multi-line text box (vertical scrollbar, or
EnterKeyBehavior true).
'Usage: Called by ContinuousUpDown.
Dim bDontDoIt As Boolean
Dim ctl As Control
Set ctl = Screen.ActiveControl
If ctl.Section = acDetail Then
If TypeOf ctl Is TextBox Then
bDontDoIt = ((ctl.EnterKeyBehavior) Or (ctl.ScrollBars > 1))
End If
Else
bDontDoIt = True
End If
Exit_ContinuousUpDownOk:
ContinuousUpDownOk = Not bDontDoIt
Set ctl = Nothing
Exit Function
Err_ContinuousUpDownOk:
If Err.Number <> 2474 Then 'There's no active control
Call LogError(Err.Number, Err.Description, conMod &
"ContinuousUpDownOk()")
End If
Resume Exit_ContinuousUpDownOk
End Function
---------------------code ends----------------------