• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Filter records on Combo Selection from another Datasource

Hi Folks,

I have a form which refers to a datamodule.  The Datamodule contains 2 TQuery objects (vwSuppliers and vwContacts).  On my form I have a grid which shows all contact records.  What I would like to do, is add a ComboBox to my form in which the user could select a Supplier name and which should then filter the vwContacts query to show only the Contact records for the selected Supplier (link field is MasSupplierID).  

Can anybody tell me how I should handle this ? I thought I've done it before, but I just can't remember how to do it.

Any help or information concerning this would be greately appreciated.

Thanks a lot in advance.

1 Solution
what is exactly the problem? ps. have you got RXLib?
StefaanAuthor Commented:
Well, my problem is that I don't know how to handle this.  Should I use a DBComboBox, a DBLookupComboBox or a simple ComboBox and fill it up myself.  I can't seem to figure out how I should fill the combobox with the values of the suppliername and when the user selects a supplier, I should pass the SupplierID of that Suppleir record to a Filter.  I've tried different things, but can't figure out how to do it.

Any help would be appreciated.

hi stefaan,

it works with a dblookupcombo,

to setup the combo

Listsource = Datasource pointed on your vwSuppliers
ListField = the field you will display
Keyfield = MasSupplierID

by the OnClick-event on this Box

procedure TForm1.DBLookupComboBox1Click(Sender: TObject);
  query2.ParamByName('SupId').AsFloat := DBLookupComboBox1.KeyValue;

This is Now with a Parameter, but you can build a filter also.
Maybe you will check that the keyvalue changed, then you should buffer the KeyValue in a global-var and compare it.

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


As long as I understand your question correctly is is relatively simple.

Your vwSuppliers query will be something like:
select * from Suppliers

As kretzschmar said use a dblookupcombo thus:
Listsource = Datasource pointed on your vwSuppliers
ListField = the field you will display
Keyfield = MasSupplierID

with no value for either datasource or datafield.

Your vwContacts query will look like this:
select * from Contacts where MasSupplierID = :MasSupplierID

And then Datasource property of the vwContacts query will be vwSupplier.

this will force a master-detail relationship, and as you slect an item from the list box the the value of the parameter (:MasSupplierID) in the detail query get changed automatically and re-executed.

This is my favorite solution as it involves no coding and happens at the dataset level


StefaanAuthor Commented:
This all seems to work quite good, but what if I want to include an option, so that the user can select ALL SUPPLIERS.

The solution proposed works fine if the user can only select one Supplier, but I would like the user to have an additional option to select all Suppliers.

Anyway, if nobody comes up with something concerning this matter, I'll use the suggestions made here, and I could divide the points over you 2 (or give you both the points).

Thanks a lot for your input.

An alternative would be to use the same set up as I suggested above, but without linking the two datasets and without a where condition on the second query.

You could then use OnFilterRecord to manually filter the records using code something like this:

procedure TForm1.Query1FilterRecord(DataSet: TDataSet;
  var Accept: Boolean);
  Accept := (DataSet['MasSupplierID'] = vwSuppliers.FieldByName('MasSupplierID').Value)

You would have to set the filtered property to true or false depending on some user input such as a check box (filtered?)

I hope this helps


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now