Need to get the Up/Down Arrow Keys Working

P

Pat Hall

I picked up support for an Access 2002 database. This is the 1st time I've
done programming. The people who use the database would lilke the Up and
Down arrow keys to work on a couple of the fields.
I think I need to put code behind the "On Key Up" and "On Key Down" Events.
I've been unable to confirm this or to find the code I need to enter.

Any help would be appreciated.
 
P

PieterLinden via AccessMonster.com

Pat said:
I picked up support for an Access 2002 database. This is the 1st time I've
done programming. The people who use the database would lilke the Up and
Down arrow keys to work on a couple of the fields.
I think I need to put code behind the "On Key Up" and "On Key Down" Events.
I've been unable to confirm this or to find the code I need to enter.

Any help would be appreciated.

How do you want them to behave?

Do you mean this?
http://www.mvps.org/access/forms/frm0041.htm
 
D

Daniel Pineault

'would lilke the Up and Down arrow keys to work'? Doing what? Incrementing
the value of a field? What do they want the key to perform as an action?

Please clarify what it is you are trying to achieve exactly.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
J

John W. Vinson

I picked up support for an Access 2002 database. This is the 1st time I've
done programming. The people who use the database would lilke the Up and
Down arrow keys to work on a couple of the fields.
I think I need to put code behind the "On Key Up" and "On Key Down" Events.
I've been unable to confirm this or to find the code I need to enter.

Any help would be appreciated.

Well, perhaps you could explain what you want the up and down arrow keys to
*do*.
 
P

Pat Hall

It's on a sub-form with several fields repeated multiple times. I need the
keys to move to the same filed either just above or just below the field they
startin. They would then key in the value they want. Like in an Excel
spreadsheet.
 
J

John W. Vinson

It's on a sub-form with several fields repeated multiple times. I need the
keys to move to the same filed either just above or just below the field they
startin. They would then key in the value they want. Like in an Excel
spreadsheet.

Well... An Access datasheet may look like an Excel spreadsheet, BUT IT ISN'T.

For one thing, data in a Table *has no defined order*. It's like a box of
rocks; there is no concept of an "above" or "below". You can only get records
in a particular sequential order by using a Query to sort records in the table
into that order.

What is the subform's Recordsource? Is there a field in that query which
specifies the sort order? If so, you can use the arrow buttons to launch an
Update Query updating that field; this can get a bit complicated in that you
may need to update multiple records not just one. What's the context? What
data is displayed on this form, and how is its sort order currently specified?
 
J

Jeanette Cunningham

Here is some code from Allen Browne

Public Sub ContinuousUpDown(frm As Form, KeyCode As Integer)
'Purpose: Respond to Up/Down in continuous form, by moving record,
' unless the active control's EnterKeyBehavior is on.
'Usage: Call ContinuousUpDown(Me, KeyCode)
On Error GoTo Err_Handler

Dim strForm As String

strForm = 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_Handler:

Exit Sub

Err_Handler:
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_Handler

End Sub

Private Function ContinuousUpDownOk() As Boolean
'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.
On Error GoTo Err_Handler
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_Handler:
ContinuousUpDownOk = Not bDontDoIt
Set ctl = Nothing

Exit Function

Err_Handler:
If Err.Number = 2474 Then 'There's no active control
Else
msgbox Err.Number & " " & Err.Description
End If
Resume Exit_Handler

End Function


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
P

Pat Hall

No; They need the Up Arrow and Down Arrow keys to work like they do in other
applications like Excel. After the cursor is in the same field on the line
above or below where they were whn the key was pressed; they will key the
value they want.
 
P

Pat Hall

Definately don't know anything about VBA programming. I copied the code to
where the the On Key Down Event Procedure code is and added code to call the
procedure.
Either nothing happens or I get this error message "The expression On Key
Down you entered as the event property setting produced the following error:
Procedure declaration does not match description of event or procedure
having the same name."

This format gives the error message:
Private Sub Deadline_KeyDown(frm As Form, KeyCode As Integer)

Call ContinuousUpDown(Me, KeyCode)

End Sub

This format does nothing: (set up when I picked "code builder".
Private Sub Deadline_KeyDown(KeyCode As Integer, shift as Integer)

Call ContinuousUpDown(Me, KeyCode)

End Sub

Could use some help on what I am doing wrong.
 
J

Jeanette Cunningham

The code that I sent goes in a new module which you can call modUpDown.
On the form where you want to use ContinuousUpDown, set the form's
KeyPreview event to Yes.

On the form's Key down event (not the control), call the function like this:

Private Sub Form_KeyDown(frm As Form, KeyCode As Integer)

Call ContinuousUpDown(Me, KeyCode)

End Sub


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
S

Stuart McCall

Jeanette Cunningham said:
The code that I sent goes in a new module which you can call modUpDown.
On the form where you want to use ContinuousUpDown, set the form's
KeyPreview event to Yes.

On the form's Key down event (not the control), call the function like
this:

Private Sub Form_KeyDown(frm As Form, KeyCode As Integer)

Call ContinuousUpDown(Me, KeyCode)

End Sub


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
<snip>

This event procedure has the wrong declaration:

Private Sub Form_KeyDown(frm As Form, KeyCode As Integer)

It should be:

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

Jeanette Cunningham

Thanks Stuart, too much hurry and not careful enough checking.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 

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