Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ClientDataSets and Lookup/Calculated Fields

640 views
Skip to first unread message

Michael Fritz

unread,
Jan 17, 2008, 9:26:23 AM1/17/08
to
Hi,

I've got two ClientDatasets which get their records from a query from
Firebird database using ZEOS library.

The first CDS holds all fields from a contacts table and one additional
field, manually added: FIRSTLASTNAME. This field is of type fkCalculated
and has assigned an OnGetText event:

procedure TdmCustomer.cdsCustContactsLASTFIRSTNAMEGetText(Sender: TField;
var Text: string; DisplayText: Boolean);
begin
Text := Sender.DataSet.FieldByName('LASTNAME').AsString + ' ' +
Sender.DataSet.FieldByName('FIRSTNAME').AsString;
end;

This works well when displaying this field inside a TDBGrid.
This does not work, when I try to perform a lookup like this one:

Val := dmCustomer.cdsCustContacts.Lookup('CONTACTSNO',
VarArrayOf([Value]), 'FIRSTLASTNAME');

1) I would expect to get the calculated value of the OnGetText event, but
get a blank string back.

2) Next, I'm trying to use this calculated field in the second CDS.
I've manually added a lookup field, FieldKind := fkLookup. The KeyField is
correctly set as well as the LookupKeyFields property.
I would like to use the calculated field 'FIRSTLASTNAME' from the first CDS
as the LookupResultField.
But I too get a blank string back instead.

Did I made something wrong or is this not possible at all?
--
cu,
Michael

Brian Bushay TeamB

unread,
Jan 17, 2008, 8:40:56 PM1/17/08
to
>I've got two ClientDatasets which get their records from a query from
>Firebird database using ZEOS library.
>
>The first CDS holds all fields from a contacts table and one additional
>field, manually added: FIRSTLASTNAME. This field is of type fkCalculated
>and has assigned an OnGetText event:
>
>procedure TdmCustomer.cdsCustContactsLASTFIRSTNAMEGetText(Sender: TField;
> var Text: string; DisplayText: Boolean);
>begin
> Text := Sender.DataSet.FieldByName('LASTNAME').AsString + ' ' +
> Sender.DataSet.FieldByName('FIRSTNAME').AsString;
>end;
>
>This works well when displaying this field inside a TDBGrid.
>This does not work, when I try to perform a lookup like this one:
>
> Val := dmCustomer.cdsCustContacts.Lookup('CONTACTSNO',
>VarArrayOf([Value]), 'FIRSTLASTNAME');
>
>1) I would expect to get the calculated value of the OnGetText event, but
>get a blank string back.
That would be an unreasonable expectation.
The value of calculated fields should be set in the dataSet's OnCalcField event

>

--
Brian Bushay (TeamB)
Bbu...@NMPLS.com

Michael Fritz

unread,
Jan 18, 2008, 3:28:48 AM1/18/08
to
"Brian Bushay TeamB" wrote in message
<news:bq00p3p0jrmmv8mrj...@4ax.com>:

>>1) I would expect to get the calculated value of the OnGetText event, but
>>get a blank string back.
> That would be an unreasonable expectation.
> The value of calculated fields should be set in the dataSet's OnCalcField event

Brian,

yes that sound at least more reasonable ;-) I now did set this event, eg:

procedure TdmCustomer.cdsCustContactsCalcFields(DataSet: TDataSet);
begin
if DataSet.State = dsCalcFields then
begin
cdsCustContactsLASTFIRSTNAME.Value := cdsCustContactsLASTNAME.Value +
cdsCustContactsFIRSTNAME.Value;
cdsCustContactsFIRSTLASTNAME.Value := cdsCustContactsFIRSTNAME.Value +
cdsCustContactsLASTNAME.Value;
end;
end;

But this event is not fired at all. As mentioned before I set FieldKind to
fkCalculated. The client dataset property AutoCalcFields is set to True,
setting it to False does not change anything though.

I'm using a TClientDateset with a TDataSetProvider which gets its records
from a query using the ZEOS library to access a Firebird database.
--
cu,
Michael

Michael Fritz

unread,
Jan 18, 2008, 4:35:01 AM1/18/08
to
"Michael Fritz" wrote in message
<news:9gr7jhth4mob.6...@40tude.net>:

> I'm using a TClientDateset with a TDataSetProvider which gets its records
> from a query using the ZEOS library to access a Firebird database.

It seems that this does not work if I us the OnCalcEvent with the
TClientDataset. When assigning the event to the query itself and do the
calculating there it works flawlessly.

--
cu,
Michael

Zoren Lendry

unread,
Jan 18, 2008, 7:07:57 AM1/18/08
to
Paul Hughes wrote:
> When supplying an OnCalcFields procedure, I would not normally check that
> State = dsCalcFields. The code will only be called when its doing the
> CalcFields anyway. I don't know if the State will be dsCalcFields at the
> point your code is called and your check may be preventing it getting to the
> point where you assign values.

I hope you are right! I have used CalcFields events thousands of times
since Delphi 1, and I have never referenced "State = dsCalcFields" from
within a CalcFields event.

Loren sZendre

Paul Hughes

unread,
Jan 18, 2008, 6:57:11 AM1/18/08
to
When supplying an OnCalcFields procedure, I would not normally check that
State = dsCalcFields. The code will only be called when its doing the
CalcFields anyway. I don't know if the State will be dsCalcFields at the
point your code is called and your check may be preventing it getting to the
point where you assign values.

Regards, Paul.

"Michael Fritz" <spam_...@yahoo.de> wrote in message
news:9gr7jhth4mob.6...@40tude.net...

Michael Fritz

unread,
Jan 18, 2008, 7:06:02 AM1/18/08
to
"Paul Hughes" wrote in message <news:47909419$1...@newsgroups.borland.com>:

> When supplying an OnCalcFields procedure, I would not normally check that
> State = dsCalcFields. The code will only be called when its doing the
> CalcFields anyway. I don't know if the State will be dsCalcFields at the
> point your code is called and your check may be preventing it getting to the
> point where you assign values.

May be but I set a break point at the line where I check for dsCalcFields
and this break is never reached either.
--
cu,
Michael

Paul Hughes

unread,
Jan 18, 2008, 9:32:43 AM1/18/08
to
Have you defined the new calculated field as a persistent field on the CDS
along with the other fields in the result set from DB dataset component?

If not, then the CDS will not call its OnCalcFields.

Also see the help on the difference between the types of calculated fields
for a CDS (fkInternalCalc and fkCalculated).

Paul.


"Michael Fritz" <spam_...@yahoo.de> wrote in message

news:ea4k0jz8i3pf.1jxr1sh5d0wnl$.dlg@40tude.net...

Ralf Jansen

unread,
Jan 18, 2008, 3:14:16 PM1/18/08
to
>
> But this event is not fired at all. As mentioned before I set FieldKind to
> fkCalculated. The client dataset property AutoCalcFields is set to True,
> setting it to False does not change anything though.
>
> I'm using a TClientDateset with a TDataSetProvider which gets its records
> from a query using the ZEOS library to access a Firebird database.

Sounds like you have defined the calculated field at the ZEOS dataset but then
trying to catch onCalcFields on your ClientDataset. Be aware that your
clientdataset will handle serverside calculated fields as fkData(IIRC all
serverside fields will become fkData fields on the clientdataset). You have to
define the calculated field at the clientdataset to to get the onCalcFields
event fired.


--
Ralf Jansen

deepinvent Software GmbH - Viersen, Germany - http://www.deepinvent.com
Archiving E-mails with MailStore: http://www.mailstore.com


Brian Bushay TeamB

unread,
Jan 19, 2008, 8:22:18 PM1/19/08
to
>> I'm using a TClientDateset with a TDataSetProvider which gets its records
>> from a query using the ZEOS library to access a Firebird database.
>It seems that this does not work if I us the OnCalcEvent with the
>TClientDataset. When assigning the event to the query itself and do the
>calculating there it works flawlessly.

Then you must have the calculated field defined in the query not in the
TclientDataset. Move the calculated field to the TclientDataset if you don't
want to set its value in the Query

0 new messages