Using 'UP' & 'DOWN' arrow keys

A

arm

Hello,

Could anyone please help me. Whenever I wanted to move to the next or
previous record in a continuous form I always use <CTRL> <PAGEDN> or <CTRL>
<PAGEUP> key combination. Is there anyway to make the <DOWN ARROW> and <UP
ARROW> keys to do the same? Thanks.
 
A

Allen Browne

Below are the two functions that we use with our continous forms. Paste them
into a standard module (Modules tab of Database window). Then in any
continuous form set these properties for the form:
Key Preview Yes
On Key Down [Event Procedure]

Click the Build button beside the On Key Down property.
Access opens the Code window.
Paste this line into the event procedure:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Call ContinuousUpDown(Me, KeyCode)
End Sub

The code responds to the Up/Down keystroke only if the control is in the
detail section of the form, and only if the control does not look like a a
multi-line control (because it has a scroll bar, or the EnterKeyBehavior is
multi-line).

The code explicitly saves any uncommitted edits (as there are bugs
associated with trying to move records with unsaved edits), and ignores the
errors of trying to move above the first or past the new record.

Replace the error handler call with your own, or if you want ours you can
grab it from here:
http://allenbrowne.com/ser-23a.html

Hope that helps.

-------------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---------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top