How can I filter a Master ClientDataSet from the Detail ClientDataset field values ?

How can I filter a Master ClientDataSet from the Detail ClientDataset field values ?

Post by Marcelo Co » Tue, 17 Oct 2006 21:56:34


Hi,

I am developing a database application with clientdatasets that loads and
saves it's data directly to and from a .cds file. I am not using any
database server.

I have a master clientdataset and a detail clientdataset.
The detail clientdataset is a datasetfield of the master clientdataset.
The detail clientdataset is a nested dataset of the master clientdataset.

The master clientdataset has a field named NumReg of integer type.
The detail clientdataset has a field named NumRegMaster of integer type.

If a detail record is inserted on the detail clientdataset the NumRegMaster
field of this record receives the same value of the NumReg field of the
corresponding record of the master clientdataset.

I want to filter the master clientdataset based on the values of some fields
of the detail dataset.

I am using the Filter and Filtered properties of the master clientdataset to
build the filter at runtime based on the filter options selected by the
user.

How can I access the detail fields of the detail clientdataset from the
master clientdataset ?

How can I filter the records of the master clientdataset based on the field
values of the detail clientdataset ?

Thank you for any help.

Marcelo.
 
 
 

How can I filter a Master ClientDataSet from the Detail ClientDataset field values ?

Post by Bill Tod » Wed, 18 Oct 2006 09:38:36


DetailCds.FieldByName('TheField').AsString;

Is that what you are asking?


There is only one master record for any detail record so I don't
understand how you can filter the master. It might help if you would
provide an example of what you are trying to do.

--
Bill Todd (TeamB)

 
 
 

How can I filter a Master ClientDataSet from the Detail ClientDataset field values ?

Post by Maxim Shir » Wed, 18 Oct 2006 22:37:16

Hi.

Nested dataset is availbale as a TDataSetField field. For example if your
nested dataset is called DetailCDS you can use

var detail : TDataSet;
...
detail := (MasterCDS.FieldByName('DetailCDS') as TDataSetField);

Now detail is your detail dataset corresponding to a selected record in the
master.

As for filters, you cannot specify detail fields in filter strings. But you
can supply an OnFilterRecord event handler. Something like this:

procedure TYourDatamodule.MasterCDSOnFilterRecord(DataSet: TDataSet; var
Accept: Boolean)
var
detail : TDataSet;
begin
detail := MasterCDS.FieldByName('DetailCDS') as TDataSetField;
if (DataSet['SomeField'] = SomeValue) and (detail.Locate('OtherField',
OtherValue, []) then begin
Accept := true;
end elese begin
Accept := false;
end
end;

This filter approximatelly correspond to

select * from master m
where
m.SomeField = :SomeValue
and
exists (select * from detail d where d.NumRegMaster = m.NumReg and
d.OtherField = :OtherValue)

Maxim.


> Hi>
> I am developing a database application with clientdatasets that loads an>
> saves it's data directly to and from a .cds file. I am not using an>
> database server>
> I have a master clientdataset and a detail clientdataset>
> The detail clientdataset is a datasetfield of the master clientdataset>
> The detail clientdataset is a nested dataset of the master clientdataset>
> The master clientdataset has a field named NumReg of integer type>
> The detail clientdataset has a field named NumRegMaster of integer type>
> If a detail record is inserted on the detail clientdataset the
NumRegMaste>
> field of this record receives the same value of the NumReg field of th>
> corresponding record of the master clientdataset>
> I want to filter the master clientdataset based on the values of some
field>
> of the detail dataset>
> I am using the Filter and Filtered properties of the master clientdataset
t>
> build the filter at runtime based on the filter options selected by th>
> user>
> How can I access the detail fields of the detail clientdataset from th>
> master clientdataset >
> How can I filter the records of the master clientdataset based on the
fiel>
> values of the detail clientdataset >
> Thank you for any help>
> Marcelo>
>
 
 
 

How can I filter a Master ClientDataSet from the Detail ClientDataset field values ?

Post by Marcelo Co » Thu, 19 Oct 2006 01:28:01

Thank you very much for your answer.

Let us explain my problem more clearly:

1) I have a masterCDS and a detailCDS which is a nested dataset of the
masterCDS.

2) I want to know which records of the masterCDS correspond to the detail
records of the detailCDS that have detail fields with certain values defined
by the user at runtime. I want to filter the masterCDS based on the values
of the detail fields of the detailCDS defined at runtime.

3) I know that exists an automatic connection between each masterCDS record
and it's corresponding detail records in the detailCDS.

4) What I want to know is:
How can I filter the masterCDS records (using the Filter property of the
masterCDS) based on the records of the detailCDS ?
Or, Given a value of a field of the detailCDS which master records
correspond to the records of the detailCDS that have this field with this
given value?

For example:

Given the masterCDS and detailCDS below,

masterCDS records:
mNumReg FirstName LastName
1 'John' 'Doe' (first master record)
2 'Mary' 'Jane' (second master record)
3 'Paul' 'Dell' (third master record)

The masterCDS has a field of TDataSetField type named
'DetailContractNumbers' that is linked to the detailCDS via its DataSetField
property.

detailCDS records:
dNumReg ContractNumber
1 '1200' (detail record of third master record)
2 '2345' (detail record of third master record)
3 '3341' (detail record of second master record)
4 '1000' (detail record of first master record)
5 '1000' (detail record of second master record)


For example: I want to use the Filter property of the masterCDS to obtain
the records of the masterCDS that have the ContractNumber field of the
detailCDS with a value equal to '1000' (this value will be defined at
runtime).

If detailCDS.FieldByName('ContractNumber') = '1000' then
the answer would be the first and second records of the masterCDS.

The string of Filter property of the masterCDS will be built at runtime.

Is it possible to access the fields of the detailCDS from the masterCDS
using the Filter property of the masterCDS ? If not how can I do that ? Is
it possible only using the OnFilterRecord of the masterCDS ?

Best regards,

Marcelo.
 
 
 

How can I filter a Master ClientDataSet from the Detail ClientDataset field values ?

Post by Marcelo Co » Thu, 19 Oct 2006 02:10:09

Hi,

Thank you for your answer.

Marcelo.




> NumRegMaste>> >> field of this record receives the same value of the NumReg field of th>> >> corresponding record of the master clientdataset>> >>> >> I want to filter the master clientdataset based on the values of som>
> field>> >> of the detail dataset>> >>> >> I am using the Filter and Filtered properties of the master clientdatase>
> t>> >> build the filter at runtime based on the filter options selected by th>> >> user>> >>> >> How can I access the detail fields of the detail clientdataset from th>> >> master clientdataset >> >>> >> How can I filter the records of the master clientdataset based on th>
> fiel>> >> values of the detail clientdataset >> >>> >> Thank you for any help>> >>> >> Marcelo>> >>> >>> >>> >>
>