Discussion:
Calculated Fields
(too old to reply)
Anderson Soffa
2008-02-05 21:43:09 UTC
Permalink
I need some help, I want to add a calculated field to a TIBClientDataSet at
runtime, and sets its command text at runtime too.

I tried to figure it out, I add the calculated field by doing this:

Field := TStringField.Create(self);
with Field do
begin
FieldName := 'Description2';
Calculated := True;
DataSet := MyDataSet;
end;

And set the commandtext with the SQL, after the open, the fields property of
the dataset only have the calculated field, the other fields appears on the
FieldDefs property, so I can't get access to them by doing FieldByName or
using the dataset['field'] operator.

What I doing wrong?

Thanks very much
Bill Todd [TeamB]
2008-02-05 20:03:26 UTC
Permalink
TIBClientDataSet has been deprecated and I strongly suggest that you do
not use it. See if the following helps.



How do I dynamically create fields from a dataset at runtime?

var
I: Integer;
Field: TField;
begin
{ Can only add fields to inactive dataset. }
Table1.Active := False;

{ Allocates field definitions if dataset has never been made active. }
Table1.FieldDefs.Update;

{ Create all fields from definitions and add to dataset. }
for I := 0 to Table1.FieldDefs.Count - 1 do
begin
{ Here is where we actually tell the dataset to allocate a field. }
{ Field gets assigned but we don't need it - just points to new
field. }
Field := Table1.FieldDefs[I].CreateField(Table1);
end;

{ This is how you can add additional calculated fields }
Field := TStringField.Create(Table1);
Field.FieldName := 'Total';
Field.Calculated := True;
Field.DataSet := Table1;

{ Now we can see our fields. }
Table1.Active := True;
--
Bill Todd (TeamB)
Anderson Soffa
2008-02-05 23:14:32 UTC
Permalink
Thanks for the help Bill, worked just as I expected.

Delphi is the best.

Anderson
Post by Bill Todd [TeamB]
TIBClientDataSet has been deprecated and I strongly suggest that you do
not use it. See if the following helps.
How do I dynamically create fields from a dataset at runtime?
var
I: Integer;
Field: TField;
begin
{ Can only add fields to inactive dataset. }
Table1.Active := False;
{ Allocates field definitions if dataset has never been made active. }
Table1.FieldDefs.Update;
{ Create all fields from definitions and add to dataset. }
for I := 0 to Table1.FieldDefs.Count - 1 do
begin
{ Here is where we actually tell the dataset to allocate a field. }
{ Field gets assigned but we don't need it - just points to new
field. }
Field := Table1.FieldDefs[I].CreateField(Table1);
end;
{ This is how you can add additional calculated fields }
Field := TStringField.Create(Table1);
Field.FieldName := 'Total';
Field.Calculated := True;
Field.DataSet := Table1;
{ Now we can see our fields. }
Table1.Active := True;
--
Bill Todd (TeamB)
Anderson Soffa
2008-02-06 00:47:46 UTC
Permalink
Post by Bill Todd [TeamB]
TIBClientDataSet has been deprecated and I strongly suggest that you do
not use it
So what's the best alternative to TIBClientDataSet considering that I'm
using Delphi 7?

Thanks

Anderson
Post by Bill Todd [TeamB]
TIBClientDataSet has been deprecated and I strongly suggest that you do
not use it. See if the following helps.
How do I dynamically create fields from a dataset at runtime?
var
I: Integer;
Field: TField;
begin
{ Can only add fields to inactive dataset. }
Table1.Active := False;
{ Allocates field definitions if dataset has never been made active. }
Table1.FieldDefs.Update;
{ Create all fields from definitions and add to dataset. }
for I := 0 to Table1.FieldDefs.Count - 1 do
begin
{ Here is where we actually tell the dataset to allocate a field. }
{ Field gets assigned but we don't need it - just points to new
field. }
Field := Table1.FieldDefs[I].CreateField(Table1);
end;
{ This is how you can add additional calculated fields }
Field := TStringField.Create(Table1);
Field.FieldName := 'Total';
Field.Calculated := True;
Field.DataSet := Table1;
{ Now we can see our fields. }
Table1.Active := True;
--
Bill Todd (TeamB)
Bill Todd [TeamB]
2008-02-05 23:16:07 UTC
Permalink
Post by Anderson Soffa
So what's the best alternative to TIBClientDataSet considering that
I'm using Delphi 7?
Separate IBDataSet, DSP and CDS components. Create a component template
and it is as easy to use separate components as TIBClientDataSet.
--
Bill Todd (TeamB)
Marc Rohloff [TeamB]
2008-02-05 22:16:43 UTC
Permalink
Post by Anderson Soffa
And set the commandtext with the SQL, after the open, the fields property of
the dataset only have the calculated field, the other fields appears on the
FieldDefs property, so I can't get access to them by doing FieldByName or
using the dataset['field'] operator.
A dataset will only create the fields for you automatically if there
are no fields defined manually. You can add them in code as Bill
suggested or you can add them from the designer by right clicking on
the datset, choosing fields editor, right click on the designer and
choose add.
--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
Loading...