How To Set a Filter Using an Item Selected from a Combo Box (139348)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 6.0

This article was previously published under Q139348

SUMMARY

This article shows by example how to use the item selected in a combo box to filter what is displayed in a second combo box.

MORE INFORMATION

The Scenario

Say you have two combo boxes on a form. The first reflects a field in the parent table, and the second reflects the child records related to the parent value picked from the possible parent values presented in the first combo box.

Step-by-Step Procedure

  1. Create a new form.
  2. Right-click the form, and click Data Environment. Then right-click the Data Environment window, and add the Customer table from \Vfp\Samples\Data. In Visual FoxPro 6.0 the Customer table can be found in home(2)+"data".
  3. Right-click the Data Environment window again, and add the Orders table from \Vfp\Samples\Data. In Visual FoxPro 6.0 the Orders table can be found in home(2)+"data".
  4. Place a combo box on the form. Change its RowSource property to Customer.cust_id and its RowSourceType property to 6 - Fields.
  5. Add the following code to the InteractiveChange event for the combo box:

       thisform.dataenvironment.cursor2.filter= ;
          "alltrim(cust_id)='"+alltrim(this.value)+"'"

  6. Place a second combo box on the form. Change its RowSource property to Orders.to_name and its RowSourceType property to 6 - Fields.
  7. Save and run the form.
  8. Select a customer ID from the first combo box. Click the down arrow on the second combo box, and note the records displayed in the second combo box relates to the customer ID you selected from the first combo box.
NOTE: If you place the line of code in Step 5 in the Valid event for the combo box, the event fires twice - once when you select an item from the combo box and again when the combo box loses the focus. Placing the code in the InteractiveChange event avoids this behavior.

Modification Type:MinorLast Reviewed:8/30/2004
Keywords:kbhowto KB139348 kbAudDeveloper