Problem with KeyPress Event

G

Guest

I have the following code on two forms. On one of the forms it works
correctly. on the other form when I press the enter key it skips a record.
Does anyone know why and how to correct the problem. Thanks in advance.

Private Sub Form_KeyPress(KeyAscii As Integer)
On Error GoTo Err_Form_KeyPress
If KeyAscii = 13 Then
KeyCode = 0
DoCmd.GoToRecord , , acNext
'set focus on QTY_PD_TO_DT field
Me.QTY_PD_TO_DT.SetFocus
End If
Exit_Form_KeyPress:
Exit Sub
Err_Form_KeyPress:
MsgBox Err.Description
Resume Exit_Form_KeyPress
End Sub
 
A

Allen Browne

There are several things that could go wrong here. For example, if the
record is dirty (uncommitted edits) and cannot be saved (e.g. required field
missing, or validation rule not met), the attempt to go ot the next record
will fail.

If the focus happened to be in the last box in the form's tab order, and the
form's Cycle property is set to the default (all records), it will move
record by itself. Your code also moves record, so that might be why it is
"skipping" a record.

You might find the KeyDown event better for non-text keystrokes. Try
something like this, replacing "MyLastbox" with the name of the last control
in your tab order:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
If Form.ActiveControl.Name <> "MyLastbox" Then
KeyCode = 0
If Me.Dirty Then
Me.Dirty = False
End If
If Not Me.NewRecord Then
RunCommand acCmdRecordsGoToNext
End If
End If
End If
End Sub
 
G

Guest

Thanks, Allen. The code worked great. It turned out that the control I was
setting focus to was last in the tab order. Thanks again.
 

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