Keyboard to move records on a continuous form

H

Hughesy

I have a number of continuous forms in my db, however my users keep asking if
they can select down the records with anything but the mouse. At present I
can use the return key to move down records in the design master, however
this feature is not available in the replicas.
Thanks for your help.
 
A

Allen Browne

I think you are trying to make a continuous form behave like a datasheet,
where pressing the Down key moves down to the next record, and presing the
Up key moves to the previous record?

You can code that by setting the KeyPreview property of your form to Yes,
and then calling the function below in the form's KeyDown event, like this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call ContinuousUpDown(Me, KeyCode)
End Sub

Access has to save the record before it can move to another, so the code
handles that. It does not move record if the active control is not in the
Detail section of the continuous form. It also does not move if the active
control looks like a multi-line one (i.e. its EnterKeyBehavior is set to
'New Line in Field', or it has a scrollbar.)

Use your own error hanlder, or grab the one in this link:
http://allenbrowne.com/ser-23a.html

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
 
Top