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

Last reviewed: October 17, 1996
Article ID: Q139348
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 5.0

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.

  3. Right-click the Data Environment window again, and add the Orders table from \Vfp\Samples\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.


Additional reference words: 5.00 3.00 VFoxWin
KBCategory: kbprg kbhowto
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 17, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.