Microsoft KB Archive/294212

= How to select controls on a subform with setFocus method in Access 2002 =

Article ID: 294212

Article Last Modified on 8/10/2004

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q294212



Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



For a Microsoft Access 97 version of this article, see 113548.

For a Microsoft Access 2000 version of this article, see 209095.

IN THIS TASK
SUMMARY
 * How to Select a Control on a Subform from a Main Form
 * How to Select a Control on a Nested Subform from a Main Form
 * How to Select a Control on a Main Form from a Subform
 * How to Select a Control on a Subform from a Nested Subform

REFERENCES



SUMMARY
You can use the SetFocus method for forms and form controls to select a field or a control on an open form, a form datasheet, a table datasheet, or a query dynaset, or to select other forms.

This article describes how to use the SetFocus method to select controls on subforms and nested subforms, and how to select controls on a main form from a control on a subform.

back to the top

How to Select a Control on a Subform from a Main Form
To select a control on a subform from a main form, do the following:
 * 1) Select the subform control.
 * 2) Select the appropriate control on the subform.

The following example demonstrates how to select a control on a subform from a main form:  Open the sample database Northwind.mdb or the sample Access project NorthwindCS.adp. Create a new, blank form that is based on the Customers table. If the field list is not visible, click Field List on the View menu. Drag the Company Name and City fields from the field list to the detail section of the form. Drag the Orders form from the Database window to the detail section of the new form. This causes Microsoft Access to embed the Orders form as a subform and to link the Orders form to the new form on the CustomerID field. Note that the Orders form contains its own subform to show order details.  Add a command button to the form with the following properties:

Name: GotoFreight

Caption: Freight

OnClick: [Event Procedure]

Set the OnClick property of the command button to the following event procedure: ' Select the Orders subform control. Me![Orders].SetFocus

' Select the Freight control on the Orders subform. Me![Orders].Form![Freight].SetFocus  Save the new form as My Customer Orders .</li> View the form in Form view, and then click the Freight button.</li></ol>

back to the top

How to Select a Control on a Nested Subform from a Main Form
To select a control on a nested subform (a subform of a subform), do the following:
 * 1) Select the subform control.
 * 2) Select the nested subform control.
 * 3) Select the appropriate control on the nested subform.

The following example demonstrates how to select a control on a nested subform from a main form: <ol>  Continuing the example above, add the following command button to the My Customer Orders form:

Name: GotoQuantity

Caption: Quantity

OnClick: [Event Procedure]

Set the OnClick property of this command button to the following event procedure: ' Select the Orders subform control. Me![Orders].SetFocus

' Select the nested Orders Subform subform control. Me![Orders].Form![Orders Subform].SetFocus

' Select the Quantity field on the nested subform. Me![Orders].Form![Orders Subform].Form![Quantity].SetFocus </li> View the form in Form view, and then click the Quantity button.</li></ol>

back to the top

How to Select a Control on a Main Form from a Subform
To select a control on a main form from a subform, do the following:
 * 1) Select the main form.
 * 2) Select the appropriate control on the main form.

The following example demonstrates how to select a control on a main form from a subform: <ol>  Continuing the example above, open the Orders form in Design view. Add the following new command button to the form:

Name: GotoCity

Caption: City

OnClick: [Event Procedure]

Set the OnClick property of this command button to the following event procedure: ' Select the main form. Forms![My Customer Orders].SetFocus

' Select the City control on the main form. Forms![My Customer Orders]![City].SetFocus </li> Save and then close the Orders form.</li> Open the My Customer Orders form in Form view.</li> Click the City button on the Orders subform.</li></ol>

back to the top

How to Select a Control on a Subform from a Nested Subform
To select a control on a subform from a nested subform, do the following:
 * 1) Select the main form.
 * 2) Select the subform control.
 * 3) Select the appropriate control on the subform.

The following example demonstrates how to select a control on a subform from a nested subform: <ol> Continuing the example above, open the Orders Subform form in Design view.</li>  Modify the Discount field's OnExit property to be:

OnExit: [Event Procedure]

Set the OnExit property of this field to the following event procedure: ' Select the main form. Forms![My Customer Orders].SetFocus

' Select the Orders subform control. Forms![My Customer Orders]![Orders].SetFocus

' Select the Freight control on the Orders subform. Forms![My Customer Orders]![Orders].Form![Freight].SetFocus </li> Save and then close the Orders Subform form.</li> Open the My Customer Orders form in Form view. Use the mouse pointer to select the Discount field in the nested Orders Subform subform. Press TAB to exit the Discount field.</li></ol>

back to the top

<div class="references_section">