Subject How to add Tfields persisntents with a TIBOquery in run-time?
Author Delmar Wichnieski
Hi all

I can't get sucessful trying to add persistents fields in run-time (not with fields editor).
I'm tried this code:

MyIBOquery.FieldDefs.Add('CODIGO',FTinteger,0,true);

but in subsequent lines like this :
nprincipal.Memo1.Lines.Add(QrecepcaoAvisosPCconcluidoCODIGO.AsString);
message error: Access violation at address 005F0983 in module myproject.exe

in this:
qrecepcaoAvisosPCconcluido.FieldByName('CODIGO').OnGetText:=QrecepcaoAvisosPCconcluidoCODIGOgettext;

Project projeto.exe raise exception class EDatabaseError with message 'field 'CODIGO' not found'.

Any comment will be very appreciated.
Thanks
Delmar - Brazil

----------------------------------------------------------------------------------------------------------
All code in this Unit thread
----------------------------------------------------------------------------------------------------------
unit TrecepcaoAvisosOS;

interface

uses
db, Classes, SysUtils, windows, IBODataset, IB_Components;

type
recepcaoAvisosOS = class(TThread)
IBODatabaseRecepcao: TIBODatabase;

IBOsessionRecepcao: TIB_session;
IBOtransactionRecepcao: TIBOTransaction;

QrecepcaoAvisosPCconcluido, QrecepcaoPCspendentes, QrecepcaoPCsentregar, Qrecepcaoaprovarorcamento, Qrecepcaofazerorcamento: TIBOQuery;
QrecepcaoAvisosPCconcluidoCODIGO: TIntegerField;



procedure QrecepcaoAvisosPCconcluidoCODIGOgettext(Sender: TField; var Text: String;
DisplayText: Boolean);

private
username, password, path, server:string;
procedure EventoOnTerminate(Sender: TObject);
protected
procedure Execute; override;
public
constructor criate(criatesuspended: Boolean; pusername, ppassword, ppath, pserver:string);
end;

implementation

uses frecepcaoAvisos, fprincipal;

{ recepcaoAvisosOS }

constructor recepcaoAvisosOS.criate(criatesuspended: Boolean; pusername, ppassword, ppath, pserver:string);
begin
password:=ppassword;
username:=pusername;
path:=ppath;
server:=pserver;
inherited Create(criatesuspended);
priority:=tplower; // tpIdle tpLowest tplower tpNormal tpHigher tpHighest tpTimeCritical
FreeOnTerminate := True;
end;

procedure recepcaoAvisosOS.QrecepcaoAvisosPCconcluidoCODIGOgettext(Sender: TField; var Text: String;
DisplayText: Boolean);
begin
if sender.AsString='1' then
text:='xxx01'
end;



procedure recepcaoAvisosOS.Execute;
var primeiravez:boolean;
var aux:string;
begin
nrecepcaoAvisos.limpagrade; //limpa a grade

IBOsessionRecepcao:=TIB_Session.Create(nil);
IBOsessionRecepcao.TimerInterval := 0;

IBODataBaseRecepcao:=TIBODatabase.Create(IBOsessionRecepcao);
IBODataBaseRecepcao.IB_Session:=IBOsessionRecepcao;

IBODataBaseRecepcao.DatabaseName :='dbrecepcao';
IBODataBaseRecepcao.Password := PASSWORD;
IBODataBaseRecepcao.Username := USERNAME;
IBODataBaseRecepcao.Path := path;
IBODataBaseRecepcao.Server := server;
//IBODataBaseRecepcao.Protocol := cpTCP_IP;
IBODataBaseRecepcao.PageSize := 4096;
IBODataBaseRecepcao.SQLDialect := 3;
IBODataBaseRecepcao.Connect;

IBOtransactionRecepcao:=TIBOtransaction.Create(IBOsessionRecepcao);
IBOtransactionRecepcao.AutoCommit:=true;

{inicio das consultas}
QrecepcaoAvisosPCconcluido := TIBOQuery.Create(IBOsessionRecepcao);
QrecepcaoAvisosPCconcluido.DatabaseName:='dbrecepcao';
QrecepcaoAvisosPCconcluido.IB_Connection:=IBODataBaseRecepcao;
QrecepcaoAvisosPCconcluido.IB_Transaction:=IBOtransactionRecepcao;
QrecepcaoAvisosPCconcluido.SQL.Clear;
QrecepcaoAvisosPCconcluido.SQL.Add('SELECT OS.CODIGO FROM OS WHERE STATUSPC='+ quotedstr('S')+ ' AND CLIENTEAVISADO IS NULL');

QrecepcaoAvisosPCconcluido.FieldDefs.Add('CODIGO',FTinteger,0 ,true); // whats! How add persistents fields?

{if I remove 2 next lines all work fine, because the code above don't add fields}
nprincipal.Memo1.Lines.Add(QrecepcaoAvisosPCconcluidoCODIGO.AsString); // in this line 'Access violation at address'
// qrecepcaoAvisosPCconcluido.FieldByName('CODIGO').OnGetText:=QrecepcaoAvisosPCconcluidoCODIGOgettext; //msg error: 'CODIGO not found'
//I suspect that for 'CODIGO not found' is because at this point I don't have open dataset yet and the correct line I fink, is the next
qrecepcaoAvisosPCconcluidoCODIGO.OnGetText:=QrecepcaoAvisosPCconcluidoCODIGOgettext;

QrecepcaoAvisosPCconcluido.Open;
primeiravez:=true;
while not QrecepcaoAvisosPCconcluido.Eof do
begin
if primeiravez=true then
begin
nrecepcaoAvisos.stringgrid1.cells[0,nrecepcaoAvisos.stringgrid1.row]:=QrecepcaoAvisosPCconcluido.FieldByName('CODIGO').AsString;
nrecepcaoAvisos.stringgrid1.cells[1,nrecepcaoAvisos.stringgrid1.row]:='Avisar cliente. PC concluído.';
nrecepcaoAvisos.stringgrid1.cells[2,nrecepcaoAvisos.stringgrid1.row]:='A';
primeiravez:=false;
end
else
begin
nrecepcaoAvisos.stringgrid1.rowcount:=nrecepcaoAvisos.stringgrid1.rowcount+1;
nrecepcaoAvisos.stringgrid1.row:=nrecepcaoAvisos.stringgrid1.rowcount-1;
nrecepcaoAvisos.stringgrid1.cells[0,nrecepcaoAvisos.stringgrid1.row]:=QrecepcaoAvisosPCconcluido.FieldByName('CODIGO').AsString;
nrecepcaoAvisos.stringgrid1.cells[1,nrecepcaoAvisos.stringgrid1.row]:='Avisar cliente. PC concluído.';
nrecepcaoAvisos.stringgrid1.cells[2,nrecepcaoAvisos.stringgrid1.row]:='A';
end;
QrecepcaoAvisosPCconcluido.Next;
end;
QrecepcaoAvisosPCconcluido.Close;

IBODataBaseRecepcao.Disconnect;
onterminate:=EventoOnTerminate;
end; //end do método execute

procedure recepcaoAvisosOS.EventoOnTerminate(Sender: TObject);
begin
if nrecepcaoAvisos.StringGrid1.Cells[0,0]<>'' then
begin
nrecepcaoAvisos.Show;
SetWindowPos(nrecepcaoAvisos.Handle,HWND_TOPMOST ,520,100,272,156, SWP_SHOWWINDOW);
end;
end;
end.


[Non-text portions of this message have been removed]