By default, Microsoft Access enables you to press the TAB key or ENTER key
to move the focus from one control to another control on a form, according
to the form's tab order. This includes moving to subform controls. However,
pressing the TAB or ENTER keys does not move the focus from a subform control back to the main form.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
- Open the sample database Northwind.mdb.
- In the Database window, click Forms under Objects and open the Orders Subform form in Design view.
- Set the OnExit property of the form's Discount control to the following event procedure:
Private Sub Discount_Exit(Cancel As Integer)
On Error Goto Error_Routine
Dim RS As DAO.Recordset
Set RS = Me.RecordsetClone
RS.MoveLast
If StrComp(Me.Bookmark, rs.Bookmark, 0) = 0 Then
Forms![Orders]![Freight].SetFocus
Forms![Orders]![Orders Subform].Requery
End If
Exit Sub
Error_Routine:
MsgBox "You must be on a record with data"
Exit Sub
End Sub
- Save and close the Orders Subform.
- Open the Orders form in Form view and press the TAB key or the ENTER key to move from one control to the next on the form. Note that the form's Freight control receives the focus when you press TAB or ENTER from the Discount control of the last record displayed on the subform.