use of up/down keys to move in form

T

TracyG

Is it possible to use the up and down keys to move between fields in a form?
I would like to be able to allow the user to go up and down within the forms
as well as moving right and left. If so, how?
 
A

Allen Browne

Presumably you are talking about a continuous form, where you want the
up/down arrows to move record like they do in Datasheet view. You can
program that in the KeyDown event of your form, though there are several
issues to take care of.

Firstly, moving record requires any changes to be saved. If that can't
happen (e.g. a required field is missing), the move cannot take place.

Secondly, some controls may be multi-line (e.g. a tall text box), and the
user may expect the down arrow to move one line down in the text box in
these controls. Typically these controls have their EnterKeyBehavior or
ScrollBars property is set.

Thirdly, there may be controls in the From Header/Footer section, and if the
focus is in one of those, the up/down arrows probably should not move
record.

The code below addresses those issues. To use it:
1. Paste the code into a standard module.
2. Set your form's KeyPreview property to Yes.
3. Set your form's KeyDown property to:
[Event Procedure]
4. In the Sub:
Call ContinuousUpDown(Me, KeyCode)

------------------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, "ContinuousUpDownOk()")
End If
Resume Exit_ContinuousUpDownOk
End Function
------------------code ends--------------------------
 
Top