Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter key moves to next record

Status
Not open for further replies.

mkrauss

Technical User
Sep 1, 2005
9
US
I have a simple time entry form with a subform that I use to enter the number of hours spent on a project each day of the week. Each new record on the main form is a new work week. Currently, everytime I press the tab key it moves to the next field. The tab key is working as I would like. However, I would like to be able to press the enter key, no matter where the current focus is at, and have the next blank record appear with the focus going to the first box on the main form.

Thanks in advance - you guys are great,

Mike
 
Set keypreview to true

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
 If KeyAscii = 13 Then
   DoCmd.GoToRecord acDataForm, Me.Name, acNext
   Me.yourControlName.SetFocus
 End If
End Sub
 
Thanks for your help, this works great when the current focus is in the main form.

What I would like to happen is that even if the current focus is in the subform and I press enter, the next main form is selected. I tried to get the code you gave me to work in the subform, but to no avail.

Also, when I press enter and I'm already at a new record, I get the following error:

Run-time error '2105';
You can't go to the specified record.

Instead of this error screen, I would like to have a message box open that says something like "There are no further records". The user would then just hit enter and the focus would go to the first control on the current new record showing. I tried messing around with an error statement in combination with the code you provided, but couldn't get it.

Thanks again for your help,

Mike
 
Make the code public so you can call it from the sub

Public Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Me.NewRecord Then
MsgBox "Your in a new record"
Else
DoCmd.GoToRecord acDataForm, Me.Name, acNext
End If
Me.ID.SetFocus
End If

Put this in the subform

Private Sub Form_KeyPress(KeyAscii As Integer)
Call Form_MainFormName.Form_KeyPress(13)
End Sub
 
Perfect - everything is working as planned. I only had to make a small change to the code entered in the subform. It was easy to fix thanks to all your examples.

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Call Form_frmTimeCard.Form_KeyPress(13)
End If
End Sub

Thanks for all your help,

Mike
 
Sorry about that, bad cut and paste job.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top