Board index » delphi » How can I add a calculated field runtime....

How can I add a calculated field runtime....

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:How can I add a calculated field runtime....


One thing you might try is something I did in one form that had to look at
data from a number of sources from the one TDbGrid - I cheated in the SQL
statements like so:

SELECT NALLOC."FILE#" AS "CODE", NALLOC.MATLOCATE AS "DESC", NALLOC.AMOUNT
AS "AMOUNT", NALLOC.DR_OR_CR AS "DC" FROM NALLOC

and

SELECT NACCOUNT.CHART AS "CODE", NCHART.REPORT_DESC AS "DESC",
NACCOUNT.AMOUNT AS "AMOUNT", NACCOUNT.DRCR AS "DC" FROM NACCOUNT, NCHART

Then all the grid had to do was have the right fields (CODE, DESC, AMOUNT
and DC) and all I had to do was change the DataSource for the Grid and it
worked perfectly, displaying data from whichever table was selected by the
DataSource.

You can of course do calculations in your SQL as well, like this

SELECT AMOUNT, (AMOUNT / 2) AS HALF FROM MYTABLE

I hope this helps
--
Christopher Latta  <cla...@ozemail.com.auX>
Remove trailing X to send non-spam emails

Floris Brouwer <flor...@euronet.nl> wrote in article
<6mn7aa$...@news3.euro.net>...

Quote
> 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:How can I add a calculated field runtime....


Quote
Floris Brouwer wrote:

> Hello there,
---snip
> 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.......:-(
--snip
> Greetings and thanks in advance,
> Floris Brouwer.

I think what you need is an expression parser to convert a
string to a number.
There are some sources and links on the TP-links page in the
"numeric" chapter and in the Delphi page.

Regards,
Franz Glaser
http://www.geocities.com/SiliconValley/2926/tp.html

Other Threads