Board index » delphi » Instantiating Fields at runtime

Instantiating Fields at runtime

Is there an easy way to instantiate fields at run time? Like a method I can
call that returns a collection of fields ?

--
Shiv Kumar
The Delphi Apostle
http://www.matlus.com:8080
http://www.delphisoap.com:8080

 

Re:Instantiating Fields at runtime


I'd also like to know how to create Calculated fields at run time....

The code I use for the previous question is:

var
  NewFieldDefs: TFieldDefs;
  NewFieldDef: TFieldDef;
  i: Integer;
begin
  NewFieldDefs := TFieldDefs.Create(ADOQuery1);
  with ADOQuery1 do
  begin
    ADOQuery1.FieldDefs.Update;
    for i := 0 to FieldCount -1 do
    begin
      NewFieldDef := NewFieldDefs.AddFieldDef;
      NewFieldDef.Name := Fields[i].FieldName;
      NewFieldDef.DataType := Fields[i].DataType;
      NewFieldDef.Size := Fields[i].Size;
      NewFieldDef.Required := Fields[i].Required;
      NewFieldDef.FieldNo := i;
    end;
    ADODataSet1.FieldDefs.Clear;
    ADODataSet1.FieldDefs.Assign(NewFieldDefs);
    ADODataSet1.CreateDataSet;
  end;

If anyone has a better solution, I'd appreciate their input.

Thanks.
--
Shiv Kumar
The Delphi Apostle
http://www.matlus.com:8080
http://www.delphisoap.com:8080

Re:Instantiating Fields at runtime


Quote
"Shiv Kumar" <sh...@erols.com> wrote in message news:3b82a8b5$1_2@dnews...
> Is there an easy way to instantiate fields at run time? Like a method I
can
> call that returns a collection of fields ?

Shiv,

Have you looked at TFieldDefList.
It's a decendant of TStringList that points to a list of field definitions
for its dataset.
I believe there is also a method GetFieldList. It should allow you to copy a
set of fields into a list object.
I hope I'm not mistaken( I often am :-(... ) but I think if you copy a field
object you also copy its definitions.
Hope this proves helpful.
Can't wait to see some websnap examples on your site!
Perhaps some of those components you gloat about will someday be free w/
source?

Rich Bakos

Re:Instantiating Fields at runtime


Quote
>Is there an easy way to instantiate fields at run time? Like a method I can
>call that returns a collection of fields ?

do you mean to create persistent tfields at runtime?

var
  f : TField;
  i : integer;
begin
 table1.FieldDefs.Update
  Table1.Close;
  for i := 0 to Table1.FieldDefs.Count - 1 do
   //create persistent field that do not exist
   if table1.FindField(table1.FieldDefs[i].Name) = nil then
      table1.FieldDefs.Items[i].CreateField(Table1);
end
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Instantiating Fields at runtime


Richard,

Thank you for your reply. The information you provided is quite useful for
some of the things I'm playing with. I didn't know of the "list" options.
I'll experiment with these ideas.

Quote
> Can't wait to see some websnap examples on your site!

Soon <g>

Quote
> Perhaps some of those components you gloat about will someday be free w/
> source?

That's the plan...

--
Shiv Kumar
The Delphi Apostle
http://www.matlus.com:8080
http://www.delphisoap.com:8080

Re:Instantiating Fields at runtime


Brian,

Thank you! Yes, this is what I think I need. Can I add calculated fields
after doing this? Frankly, I haven't figured out how one can create
calculated fields at run time.

My need really is to create calculated fields. I soon realized, that I had
to first create all existing fields (as is apparent at design time) and then
get on to calculated fields. So currently, I'm still stuck on calculated
fields.

There must be something basic I'm missing.

--
Shiv Kumar
The Delphi Apostle
http://www.matlus.com:8080
http://www.delphisoap.com:8080

Re:Instantiating Fields at runtime


Hi Shiv,

You are right about this. After adding your persistent fields you can add a
calculated field. For example:

procedure AddCalculatedStringField(ADataSet: TDataSet; const FldName:
string);
begin
  with TStringField.Create(ADataSet) do
    begin
      FieldKind := fkCalculated;
      FieldName := FldName;
      Calculated := True;
      Name := ADataSet.Name + FieldName;
      Size := 255;
      DataSet := ADataSet;
    end;
end;

Martijn Houtman

Quote
"Shiv Kumar" <sh...@erols.com> wrote in message news:3b8332aa$1_2@dnews...
> Brian,

> Thank you! Yes, this is what I think I need. Can I add calculated fields
> after doing this? Frankly, I haven't figured out how one can create
> calculated fields at run time.

> My need really is to create calculated fields. I soon realized, that I had
> to first create all existing fields (as is apparent at design time) and
then
> get on to calculated fields. So currently, I'm still stuck on calculated
> fields.

> There must be something basic I'm missing.

> --
> Shiv Kumar
> The Delphi Apostle
> http://www.matlus.com:8080
> http://www.delphisoap.com:8080

Re:Instantiating Fields at runtime


Quote
>Thank you! Yes, this is what I think I need. Can I add calculated fields
>after doing this? Frankly, I haven't figured out how one can create
>calculated fields at run time.

var
  f : TField;
  i : integer;
begin
 table1.FieldDefs.Update
  Table1.Close;
  for i := 0 to Table1.FieldDefs.Count - 1 do
   //create persistent field that do 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.DisplayLabel := 'CalcField';
  f.Calculated := True;
  f.DataSet := Table1;
  Table1.Open;
end;
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Instantiating Fields at runtime


Thank you both (Martin and Brian),

Brian,

I had tried this method (the one you depicted) but I believe the OnCalcField
event is not fired in this case. I haven't tried Martin's way yet.

--
Shiv Kumar
The Delphi Apostle
http://www.matlus.com:8080
http://www.delphisoap.com:8080

Re:Instantiating Fields at runtime


Quote
>I had tried this method (the one you depicted) but I believe the OnCalcField
>event is not fired in this case. I haven't tried Martin's way yet.

You still have to write an Oncalcfield event for the table but it will be called
if it is not set to nil

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads