ginsonic
asked on
Create a calculated field at runtime and more
I use next code to create my runtime calculated field:
Path:=ExtractFilePath(Appl ication.Ex eName);
Table1.TableName:=Path+'Da tabase\myO my.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?
Path:=ExtractFilePath(Appl
Table1.TableName:=Path+'Da
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,
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?
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 ;-)
>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 ;-)
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 ;-)
set on all other TFields the visible-property to false
meikl ;-)
ASKER
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 :(
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 ;-)
>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 ;-)
ASKER
Don't work with
Table1.FieldDefs.Update; //Retrieve all Fields
And still wish to KNOW how can add a column at runtime.
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
will try this evening myself
ASKER
I use now:
Path:=ExtractFilePath(Appl ication.Ex eName);
Table1.TableName:=Path+'Da tabase\myO my.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.Fi eldDefs[i] .Name) = nil then
table1.FieldDefs.Items[i]. CreateFiel d(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:=CalcM E;
Grid1.Columns.Add.FieldNam e:='CalcFi eld';
Grid1.Columns.Add.Width:=2 50;
Path:=ExtractFilePath(Appl
Table1.TableName:=Path+'Da
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.Fi
table1.FieldDefs.Items[i].
{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:=CalcM
Grid1.Columns.Add.FieldNam
Grid1.Columns.Add.Width:=2
well, tried myself a bit->not so easy
found following article
http://bdn.borland.com/article/0,1410,16467,00.html
meikl ;-)
found following article
http://bdn.borland.com/article/0,1410,16467,00.html
meikl ;-)
ASKER
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 :)
The last code that I put work,but don't know if the DBGrid add column is very correct :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thamks for asistance.
ooops,
just forgotton this q,
sorry about this
thanks for the points :-))
hope it works now like a charme
meikl ;-)
just forgotton this q,
sorry about this
thanks for the points :-))
hope it works now like a charme
meikl ;-)
Path:=ExtractFilePath(Appl
Table1.TableName:=Path+'Da
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,
end;
Table1.Open;
not tested
meikl ;-)