Acces 97 and keynavigation in continuous sub-forms

E

Erwin Roos

Hello

I made a form where make use of the 'key-down' form function.
This because scrolling through the records by default within continuous
forms is from left to right and top to bottom pressing the 'down-arrow' key.
On 'key-down' I examine the key-code of the key pressed and I take actions
to position the record/field pointer.

This all works fine in the form. Pressing:
Up-arrow = previous record
Down-arrow = next record
Left-arrow = next field
Left-arrow = previous field

As soon as I create an form and paste the above described form as subform
the record/field scroll functions do not work anymore.
The keys just function as they where by default.


Thanx in advance,



Erwin
 
A

Allen Browne

Erwin, it's a bit hard to debug when we do not know what your code is using
to move record, but it probably has to do with what has focus at the time,
and what actions you are applying.

There are several things that can go wrong with the attempt to move, e.g.
the record may be in a state where it cannot be saved (e.g. a required field
is missing, or it would create a duplicate), but here's what I use to
achieve this.

1. Paste the code below into a standard module, and save.

2. Set your continuous form's KeyPreview property to Yes.

3. Set the form's On Key Down property to:
[Event Procedure]
Click the Build button beside this to open the code window.
Between the "Private Sub..." and "End Sub" lines enter:
Call ContinuousUpDown(Me, KeyCode)


This is the code to go in the standard module:
-------------------code begins-------------------------------
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)

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
MsgBox Err.Number & " - " & Err.Description
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
MsgBox Err.Number & " - " & Err.Description
End If
Resume Exit_ContinuousUpDownOk
End Function

---------------------code ends--------------------------------
 
A

Albert D. Kallal

You likely forgot to set the forms keyPreview to yes.

And, the code I used for years:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

' key hand

Select Case KeyCode

Case vbKeyUp
KeyCode = 0
On Error Resume Next
DoCmd.GoToRecord acActiveDataObject, , acPrevious

Case vbKeyDown
KeyCode = 0
On Error Resume Next
DoCmd.GoToRecord acActiveDataObject, , acNext

End Select

End Sub

You don't need much code at all.
 
E

Erwin Roos

Thanx it works...
I used acDataForm instead of acActiveDataObejct with the DoCmd.GoToRecord
function.


Regards,


Erwin
 
T

tom

Hey All,

I have just started using Allen's VERY HANDY
up/down-navigation-by-arrow-keys code for continuous forms (thanks,
Allen). There's one tweek I'd love to make, but I can't figure it out.
When seeing if up/down is ok to do, it checks the control you're in
like so:

If TypeOf ctl Is TextBox Then
bDontDoIt = ((ctl.EnterKeyBehavior) Or (ctl.ScrollBars >
1))
End If

Now, I notice that when I've implemented this and I'm in a combo box
that is dropped down, the arrow keys are moving me to the next/prior
record instead of moving about the dropped-down list. Drat. It's easy
enough to see if we're in a combo box (if typeof ctl is
access.combobox), but HOW CAN I TELL IF THE COMBO BOX IS DROPPED DOWN?
I'm guessing it's impossible, but perhaps... (I can hope can't I?)

Note: I'm working in Access 2002.

Thanks,
-Tom
 

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