Link to home
Start Free TrialLog in
Avatar of ginsonic
ginsonicFlag for Romania

asked on

Create a calculated field at runtime and more

I use next code to create my runtime calculated field:

  Path:=ExtractFilePath(Application.ExeName);
  Table1.TableName:=Path+'Database\myOmy.dbf';
  Table1.IndexName:='myOmy';
  MyField := TFloatField.Create(Table1);

  with MyField do
   begin
    FieldName := 'MyField';
    Calculated := True;
    DataSet := Table1;
    Name := Table1.Name + FieldName;
    Table1.FieldDefs.Add(Name, ftString, 250, false);
   end;
   Table1.Open;

  But I need to use the others fields of the table and I don't know how to add these.

   My table have , for example, Field1 and Field2. If I try to use the calculated field with Field2, I get an error that the Field2 don't exist. How to add?

    In same time this calculated field is used inside a DBGrid. How to add a column at runtime for this field?
Avatar of kretzschmar
kretzschmar
Flag of Germany image

try

Path:=ExtractFilePath(Application.ExeName);
 Table1.TableName:=Path+'Database\myOmy.dbf';
 Table1.IndexName:='myOmy';
 
  //
 Table1.FieldDefs.Update; //Retrieve all Fields
  //

 MyField := TFloatField.Create(Table1);
 with MyField do
  begin
   FieldName := 'MyField';
   Calculated := True;
   DataSet := Table1;
   Name := Table1.Name + FieldName;
   Table1.FieldDefs.Add(Name, ftString, 250, false);
  end;
  Table1.Open;

 not tested

meikl ;-)
appendix

>If I try to use the calculated field with Field2
use the FieldByName-method

>In same time this calculated field is used inside a DBGrid
don't use predefined columns, the dbgrid does then automatically add your field

meikl ;-)
Avatar of ginsonic

ASKER

I wish to show just this field inside my DBGrid
>I wish to show just this field inside my DBGrid
set on all other TFields the visible-property to false

meikl ;-)
I think that will be easier to add a column and not to set more than 30 fields visibility to false :)

Still don't know how to create this at run-time :(
>I think that will be easier to add a column and not to
>set more than 30 fields visibility to false :)

?really

for i := 0 to table1.fieldcount-2 do
  table1.fields[i].visible := false;

thats all

meikl ;-)
Don't work with

Table1.FieldDefs.Update; //Retrieve all Fields

And still wish to KNOW how can add a column at runtime.
no delphi on hand yet,
will try this evening myself
I use now:

  Path:=ExtractFilePath(Application.ExeName);
  Table1.TableName:=Path+'Database\myOmy.dbf';
  Table1.IndexName:='myOmy';
  Table1.Open;
  table1.FieldDefs.Update;
  Table1.Close;

  for i := 0 to Table1.FieldDefs.Count - 1 do
   {create persistent field that does not exist}
    if table1.FindField(table1.FieldDefs[i].Name) = nil then
      table1.FieldDefs.Items[i].CreateField(Table1);
  {create a calculated field}
  f := TStringField.Create(Table1);
  f.Name := 'Table1CalcField';
  f.FieldName := 'CalcField';
  f.Size:=250;
  f.Calculated := True;
  f.DataSet := Table1;
  Table1.Open;
  Table1.OnCalcFields:=CalcME;

  Grid1.Columns.Add.FieldName:='CalcField';
  Grid1.Columns.Add.Width:=250;
well, tried myself a bit->not so easy

found following article

http://bdn.borland.com/article/0,1410,16467,00.html

meikl ;-)
If you read my initial code is the same :)

The last code that I put work,but don't know if the DBGrid add column is very correct :)
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thamks for asistance.
ooops,
just forgotton this q,
sorry about this

thanks for the points :-))

hope it works now like a charme

meikl ;-)