Board index » delphi » Add fields to TQuery / TTable dynamically at run-time

Add fields to TQuery / TTable dynamically at run-time

Scott,

There's a little bit of a trick to this, which is to add the fields
you want before opening the table.  Something like this:

var F : TField;
begin
  if Table1.Active then Table1.Close;
  Table1.FieldDefs.Update;
  F := TStringField.Create(Self);
  F.Name := 'afield';
  F.FieldName := 'afield';
  F.FieldKind := fkData;
  F.Size := 20; // or whatever, but should match underlying table
                // field
  F.DataSet := Table1;

Fwiw, this works even if you have persistent fields defined, and with
TQueries.

Cheers, Martyn

 

Re:Add fields to TQuery / TTable dynamically at run-time


Of course there is a way!

Table1.FieldDefs.Add(fieldname, fieldtype, fieldsize, required);

and you have made a field at runtime!! Congratulations! heheh

Nirav Kaku

Quote
sottesen wrote:

> Hello-

> When you place a TQuery or TTable component on a form, you can
> double-click on it and "add" fields that you can easily access with the
> Pascal.  No problem at design-time.  Now, how do I do this dynamically
> at run-time?  Is there a way?

> Thanks!!!!!!!!

> Scott Ottesen
> sotte...@wasatch.com

Other Threads