Filter records on Combo Selection from another Datasource

Posted on 1998-11-16
Last Modified: 2010-04-04
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.

Question by:Stefaan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1346994
what is exactly the problem? ps. have you got RXLib?

Author Comment

ID: 1346995
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.

LVL 27

Expert Comment

ID: 1346996
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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1346997

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



Author Comment

ID: 1346998
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.

Accepted Solution

zac earned 100 total points
ID: 1346999

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



Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question