Page 1 of 1

Problem override TIBXQuery.CreateFields method

Posted: Thu 11 Aug 2011 20:40
by adm.nilser
Hi!

I ordered the IBDAC Standard and i'm trying to create a class inherited from TIBCQuery that override the CreateFields method. My intention is add a event calling OnCreateFields into this method to add calculated fields at runtime.

Code: Select all

TIBXQuery = class(TIBCQuery)
protected
  procedure CreateFields; override;
public
  property OnCreateFields: TNotifyEvent read fOnCreateFields write fOnCreateFields;
end;

procedure TIBXQuery.CreateFields;
begin
  inherited CreateFields;
  if Assigned(fOnCreateFields) then
    fOnCreateFields(Self);
end;


however when i add a calculated field on event not works. the application returns the error: "The fields is not found".

Any idea about what is happening??

Posted: Fri 12 Aug 2011 07:56
by AndreyZ
Hello,

The point is that we create internal description objects for all fields, allocate memory for them, and use them for working with fields. You should create dynamic fields before opening the TIBCQuery (or TIBXQuery) component instead of overriding the OnCreateFields method. Here is an example:

Code: Select all

procedure TMainForm.BitBtn1Click(Sender: TObject);
var
  str: TStringField;
  i: integer;
begin
  IBCQuery1.FieldDefs.Update;
  for i := 0 to IBCQuery1.FieldDefs.Count - 1 do
    IBCQuery1.FieldDefs[i].CreateField(IBCQuery1);
  str := TStringField.Create(IBCQuery1);
  str.FieldName := 'test';
  str.FieldKind := fkCalculated;
  str.DataSet := IBCQuery1;
  IBCQuery1.Open;
end;

Posted: Fri 12 Aug 2011 14:44
by adm.nilser
Hi, how can I insert this event in a inherited class?
I need to make this, because i'm replacing the DBX components by IBDAC, and this code works fine with TSQLQuery inheritance.
There's a method that I can override and add this event?

Posted: Mon 15 Aug 2011 10:00
by Dimon
IBDAC allows you to create calculated fields only as demonstrated in the example above. You can do it in the BeforeOpen event handler instead of OnCreateFields.

Posted: Mon 15 Aug 2011 16:08
by adm.nilser
Ok! Can I use de NeedCreateFieldDefs on BeforeOpen to check wether fields already was created??

Posted: Tue 16 Aug 2011 07:06
by Dimon
The NeedCreateFieldDefs method will always return True in the BeforeOpen event handler.