Board index » delphi » run-time adding a calculated field

run-time adding a calculated field

Hello there,
I'm really hoping someone could help me out..... ...I'm really stuck....
I made a tool for the company I'm working for, with this tool every
sub-dataset can be retreived with an SQL-statement from three different
tables.
The SQL-statement is build runtime, so I canNOT use the columnseditors (for
the grid and the query).... ....an exception rises in case not all fields
are in the SQL-statement. Invisible fields are no solution, because
sometimes CUM(fieldname) is placed in the SQL-statement.
I use the following two functions to add fields runtime:
procedure TForm1.AddColumn(Naam: String;Titel: String; var Grid: TDBGrid);
var
  Kolom : TColumn;
begin
  Kolom := TColumn.Create(Grid.Columns);
  Kolom.FieldName := Naam;
  Kolom.Title.caption := Titel;
  //....
end;
procedure TForm1.AddQueryVeld(Naam: String; ftType : TFieldType; var Query:
TQuery);
begin
   // check here FtType for size.
   TFieldDef.Create(Query.FieldDefs, Naam, ftType, 25, False,
Query.FieldDefs.Count);
 end;
Before a new query becomes active I use:
DM.resultaatQuery.Fielddefs.clear;
DBGrid1.Columns.RebuildColumns;
One of the fields contains an integer value, the value has to be divided by
60. My idea was to add a calculated field (runtime!) I tried this in many
ways..... .....nothing worked till now.......:-(
When I do something like:
with TFloatField.Create(dmSSP.resultaatQuery) do
    begin
    FieldName := 'Uren';
    FieldKind:= fkCalculated;
    Name := 'Uren';
    dmSsp.resultaatQuery.FieldDefs.Add('Uren', ftFloat, 0, false);
    end;
AddColumn('Uren','Uren',DBGrid1);
I can't use the following line in my SQL.....
dmSSP.resultaatQuery.SQL.Add('Uren');
Another problem is that with everything build run-time, the usage of{*word*20}in
my SQL-statement leaves the other field empty.....
I really really hope someone could help me with some example-code, a
refreshing idea, or maybe an answer to the question: 'What am I doing
wrong......?'.  I spended my half weekend on this problem and still no
solution......
I wouldn't mind to sent you my source-code if you think it's interesting.
Greetings and thanks in advance,
Floris Brouwer.

 

Re:run-time adding a calculated field


Im Artikel <6mnvn9$...@news3.euro.net>, "Floris Brouwer" <flor...@euronet.nl>
schreibt:

Quote
>The SQL-statement is build runtime, so I canNOT use the columnseditors (for
>the grid and the query).... ....an exception rises in case not all fields
>are in the SQL-statement.

Hello!

I think, I have a particularly solution for your problem:

procedure TForm1.BuildQuery;
var
  Einheiten: TFieldDef;
begin
  Query := TQuery.Create(Self);
  Query.DataBaseName := DataBaseName;
  with Query do begin
    with SQL do begin
      Add('SELECT DATUM, ANFANG, DAUER, ZIELNR, EINHEITEN, FREMD');
      Add('FROM "TELERECH.DBF"');
    end;
    if Berechnen1.Checked then begin
      SQL[0] := SQL[0] + ', :BEinheiten';
      OnCalcFields := CalcFields;
      FieldDefs.Update;
      FieldByName('BEinheiten').Calculated := True;
    end;
    Prepare;
    Open;
  end;
end;

Not declared identifiers within this method are fields of TForm1. The Query
knows the calculated field BEinheiten but it cannot calculate it. I get the
message that the type of the field BEinheiten is unknown. Perhaps do you have a
solution. It is possible to set the field type of the new TFieldDef and TField
components but it doesn't solve the problem.

Andreas
Internet: http://members.aol.com/AJakobsche/

Other Threads