Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

HOW TO: Select Controls on Subform with SetFocus Method in Access 2000


View products that this article applies to.

Summary

You can use the SetFocus method for forms and form controls in Microsoft Access to select a field or control on an open form, form datasheet, table datasheet, or 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.

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:
  1. Open the sample database Northwind.mdb or the sample Access project NorthwindCS.adp.
  2. Create a new, blank form based on the Customers table.
  3. 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.
  4. Drag the Orders form from the Database window onto the detail section of the new form. Microsoft Access will embed the Orders form as a subform, linking the Orders form to the new form on the CustomerID field. Note that the Orders form contains its own subform to show order details.
  5. Add a command button to the form with the following properties:
    Name: GotoFreight
    Caption: Freight
    OnClick: [Event Procedure]
    Create the following code for the OnClick property's event procedure:
    ' Select the Orders subform control.
    Me![Orders].SetFocus
    
    ' Select the Freight control on the Orders subform.
    Me![Orders].Form![Freight].SetFocus
    					
  6. Save the new form as My Customer Orders.
  7. View the form in Form view, and then click the Freight button.

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:
  1. Continuing the example above, add the following command button to the My Customer Orders form:
    Name: GotoQuantity
    Caption: Quantity
    OnClick: [Event Procedure]
    Create the following code for the OnClick property's 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
    					
  2. View the form in Form view, and then click the Quantity button.

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:
  1. 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]
    Create the following code for the OnClick property's 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
    					
  2. Save and then close the Orders form.
  3. Open the My Customer Orders form in Form view.
  4. Click the City button on the Orders subform.

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:
  1. Continuing the example above, open the Orders Subform form in Design view.
  2. Modify the Discount field's OnExit property to be:
    OnExit: [Event Procedure]
    Create the following code for the OnExit property's 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
    					
  3. Save and close the Orders Subform form.
  4. Open the My Customer Orders form in Form view. Use the pointer to select the Discount field in the nested Orders Subform subform. Press TAB to exit the Discount field.



↑ Back to the top


References

For more information about the SetFocus method, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type SetFocus method (Access) in the Office Assistant or the Answer Wizard, and then click Search to view the topic.










↑ Back to the top


Keywords: KB209095, kbdta, kbhowtomaster, kbhowto

↑ Back to the top

Article Info
Article ID : 209095
Revision : 1
Created on : 12/12/2002
Published on : 12/12/2002
Exists online : False
Views : 298