Subject Re: [IBO] How add calculated fields at R/T w/ default fields??
Author Helen Borrie
At 01:32 AM 03-11-01 +0000, you wrote:
>I have a situation where I create a TIBODataset (like a TQuery
>descended from TDataset) in code and would like to add some calculated
>to the result set. This needs to be done on the client at run-time.

I'm not sure that TDataset lets you create persistent fields at run-time...but, assuming it does, read on for a few more comments..

>am not doing any of this visually and the default fields from the
>server plus my
>added calculated fields are all I need.
>I have tried several ways and the fields seem to add to the fields
>list of the dataset but the calculated fields I add have an
>Field.Index of -1
>regardless of how I try to assign the index value.
>Here is what I have tried:
>//TPaymentsData is an TIBODataset descendant, which is a TDataset
>procedure TPaymentsData.DoAfterOpen;

I'm sure that you could not add a new object to the dataset after the dataset is open. At the latest, I would think (if you could do it at all) it would have to be (a) before Prepare and (b) while the dataset is in an unprepared state.

> var
> Fld : TField;
> begin
> // Add WHCode calcd field
> Fld := TField.Create(Self);
> Fld.FieldKind := fkCalculated;
> Fld.FieldName := 'WHCodeCalc';
> Fld.Calculated := True;
> Fld.Index := Fields.Count; // <-- problem: does not set, stays at
>-1 ??

Fields.Count won't be available if the dataset isn't prepared. If it is returning -1 in the AfterOpen event then it looks as if your dataset isn't even prepared, let alone open.

Also, I notice that you haven't assigned a FieldType to the calculated field and you also haven't subclassed TField to the descendant you want. TField is an abstract base class so you must do this.

As I said, I don't know whether TDataset lets you add to the Fields[] array at runtime *at all*. What requirement do you have that prevents you from creating the calculated field at design time?


All for Open and Open for All
InterBase Developer Initiative ยท