Solved

Filter records on Combo Selection from another Datasource

Posted on 1998-11-16
6
229 Views
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.

0
Comment
Question by:Stefaan
6 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1346994
what is exactly the problem? ps. have you got RXLib?
0
 
LVL 3

Author Comment

by:Stefaan
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.

0
 
LVL 27

Expert Comment

by:kretzschmar
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);
begin
  query2.close;
  query2.ParamByName('SupId').AsFloat := DBLookupComboBox1.KeyValue;
  query2.open;
end;

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.

meikl
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:zac
ID: 1346997
Stefaan,

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

Cheers,

Zac
0
 
LVL 3

Author Comment

by:Stefaan
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.
0
 
LVL 1

Accepted Solution

by:
zac earned 100 total points
ID: 1346999
Stefaan

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);
begin
  Accept := (DataSet['MasSupplierID'] = vwSuppliers.FieldByName('MasSupplierID').Value)
end;

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

Cheers,

zac
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…

820 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