Board index » delphi » HELP!!: Creating TField component at run-time.

HELP!!: Creating TField component at run-time.

Help!   I am trying to write a .DLL that includes some database
functions.  I have 2 functions (units) now that run when associated
with a form and i can use the fields editor to create the TField
components.  But I dont want to show a form, I want this to be just a
function.

I have been successful in creating the TTable component at run time
and accessing the fields via TABLE.FIELDS[x].  but I need a way to
create the TField components at run-time so that i can access the
fields by name rather than by column position.  I have tried the
following:

   TC_FieldName := TStringField.Create(nil);

This compiles, but then does not access the field in the table TC.
Then I added the following:

   TC_FieldName.DataSet := TC;
   TC_FieldName.FieldName := 'FIELD NAME';

This still compiled, but I still couldnt access the field in TC.
I tried changing the Create line to the following:

   TC_FieldName := TStringField.Create(TC);

Still compiled, but caused a GPF.

Im not even sure that this component needs to be "created" in this
manner since the online help is NON-EXISTENT for dynamically creating
TField objects!

Please tell me what Im doing wrong!!!

Thanks in advance.
Marc

 

Re:HELP!!: Creating TField component at run-time.


In article <4hcflk$...@peach.america.net>,
   mob...@america.net (Marc P. Oburg) wrote:

Quote
>Help!   I am trying to write a .DLL that includes some database
> [snip]
>following:

>   TC_FieldName := TStringField.Create(nil);

>This compiles, but then does not access the field in the table TC.
>Then I added the following:

>   TC_FieldName.DataSet := TC;
>   TC_FieldName.FieldName := 'FIELD NAME';

> [snip]

Marc,

If you have any persistent fields (made with the field editor) then
you should do it as follows:

var
  fld: TDateTimeField; { just happened to be the table }
  i: Integer;
begin
  with Table1 do begin
    if Active then Close;
    FieldDefs.Update; { table must be closed for this }
    { make sure we have the field }
    i := FieldDefs.IndexOf( 'Updated_Date' );
    if i < 0 then raise Exception.Create( 'error' );
    { create the field }
    fld := FieldDefs[i].CreateField( Table1 ) as TDateTimeField;
    { prove it works }
    Open;
    ShowMessage( fld.AsString );
    end;
end;

Ryan VanIderstine

Other Threads