Problem override TIBXQuery.CreateFields method

Discussion of open issues, suggestions and bugs regarding IBDAC (InterBase Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
adm.nilser
Posts: 10
Joined: Sat 09 Jul 2011 15:53
Location: Brasil

Problem override TIBXQuery.CreateFields method

Post by adm.nilser » Thu 11 Aug 2011 20:40

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??

AndreyZ

Post by AndreyZ » Fri 12 Aug 2011 07:56

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;

adm.nilser
Posts: 10
Joined: Sat 09 Jul 2011 15:53
Location: Brasil

Post by adm.nilser » Fri 12 Aug 2011 14:44

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?

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Mon 15 Aug 2011 10:00

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.

adm.nilser
Posts: 10
Joined: Sat 09 Jul 2011 15:53
Location: Brasil

Post by adm.nilser » Mon 15 Aug 2011 16:08

Ok! Can I use de NeedCreateFieldDefs on BeforeOpen to check wether fields already was created??

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 16 Aug 2011 07:06

The NeedCreateFieldDefs method will always return True in the BeforeOpen event handler.

Post Reply