Subject Solved! Cool technique for your toolbox... WAS: Re: [IBO] Accessing & Adding Fields at runtime
Author Rob Schuff
Jason and all,

just put this together and doing it in the AfterPrepare seems to work well.
The key concept is to use the fielddefs to instantiate TFields. This does a
kind of quasi-persistent field creation thing.

this code works against a connection to employee.gdb with qry.sql set to
select* from customer. just code AfterPrepare and the CalcFields event
handlers like this:

procedure TForm1.qryAfterPrepare(Sender: TObject);
var
i: Integer;
aField: TField;
begin
// After a prepare, the fielddefs are loaded
// qry.fieldCount=0 at this point, so the TFields haven't been created
yet.
// So we'll create the quasi-persistant fields now.

// Create fields from field defs and add to qry
for i:=0 to qry.FieldDefs.Count - 1 do begin

// instantiate a TField from the field def
aField := qry.FieldDefs[i].CreateField(qry);

// once the field is instantiated you can alter it properties
if UpperCase(aField.FieldName)='CONTACT_FIRST' then begin
aField.DisplayLabel:='First';
end;
if UpperCase(aField.FieldName)='CONTACT_LAST' then begin
aField.DisplayLabel:='Last';
end;
end;

// This is how you can add calculated fields
aField := TStringField.Create(qry);
aField.FieldName := 'Contact_Full';
aField.Calculated := True;
aField.DataSet := qry;
end;

procedure TForm1.qryCalcFields(DataSet: TDataSet);
begin
if qry.findField('Contact_Full')<>nil then begin
qry.FieldByName('Contact_Full').AsString:=
qry.FieldByName('Contact_First').AsString+' '+
qry.FieldByName('Contact_Last').AsString;
end;
end;

*************

Rob

----- Original Message -----
From: "Jason Wharton" <jwharton@...>
To: <IBObjects@yahoogroups.com>
Sent: Thursday, November 28, 2002 8:32 AM
Subject: Re: [IBO] Accessing & Adding Fields at runtime


> AfterPrepare is probably too late.
> I'm not really sure what will do it but I'm interested in what you find
out.
>
> Jason Wharton
> CPS - Mesa AZ
> http://www.ibobjects.com
>
> -- We may not have it all together --
> -- But together we have it all --
>
>
> ----- Original Message -----
> From: "Rob Schuff" <rob@...>
> To: <IBObjects@yahoogroups.com>
> Sent: Wednesday, November 27, 2002 4:32 PM
> Subject: [IBO] Accessing & Adding Fields at runtime
>
>
> > hi folks,
> >
> > I have created a component that contains a TIBOQuery. There is no
> > design-time creation of TFields. I'd don't need persistent fields but I
> do
> > need to deal with some issues and I'm asking what event to hook
> into/surface
> > to accomplish the following:
> >
> > 1. Define a calculated field and code the OnCalsFieldsEvent
> > 2. Access the fields to set some of the tfield properties such as
> > displayLabel etc.
> >
> > and lastly
> >
> > 3. I need the embedded query to not instantiate fields of type
> > TIBOFloatField but instead use the normal TFloatField.
> >
> > Seems like the afterprepare is the place to do it.
> >
> > Any suggestions greatly appreciated!
> >
> > rob
>
>
>
>
>
___________________________________________________________________________
> IB Objects - direct, complete, custom connectivity to Firebird or
InterBase
> without the need for BDE, ODBC or any other layer.
>
___________________________________________________________________________
> http://www.ibobjects.com - your IBO community resource for Tech Info
papers,
> keyword-searchable FAQ, community code contributions and more !
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>