FDQUERY at runtime

0

I am trying to use FDQuery at run-time only. I've done a lot of research and tried several changes, but all of them ends with Access Violetion , so I suspect that the component is not instantiating correctly. I looked for examples on the internet to study and understand my mistake, and until that moment I did not find anything I did not do.

I leave here my example of how the code is now, and please ask someone to point out where it is wrong.

private
  { Private declarations }


public
  { Public declarations }
  FDInsertForn : TFDQuery;
end;

implementation

procedure TFLancamento.EdRazaoExit(Sender: TObject);
const
SQLInsert : String =  'INSERT INTO FOR1A' + sLineBreak +
                                   '( CNPJ,FANTASIA,RAZAO)VALUES' + sLineBreak +
                                   '( :CN, :FANTASIA, :RAZAO)';
begin
  FDInsertForn.Connection:= UDM.FDConexao;
  FDInsertForn.SQL.Clear;
  FDInsertForn.SQL.Add(SQLInsert);
  FDInsertForn.Params.ParamByName('CN').AsString:= dado;
  FDInsertForn.Params.ParamByName('FANTASIA').AsString:= EdFantasia.Text;
  FDInsertForn.Params.ParamByName('RAZAO').AsString:= EdRazao.Text;
  FDInsertForn.ExecSQL; 
end;
    
asked by anonymous 16.12.2018 / 13:37

1 answer

2

This problem most likely occurs because your FDInsertForn is not being instantiated. Just adding the instantiation will solve your problem:

FDInsertForn := TFDQuery.Create(nil);

But in this code there are other points to be commented out, as you are creating your TFDQuery at runtime, it is not necessary to declare it on the form if it will only be used in a function, for example:

procedure TFLancamento.EdRazaoExit(Sender: TObject);
const
  SQLInsert : String =  'INSERT INTO FOR1A' + sLineBreak +
                                   '( CNPJ,FANTASIA,RAZAO)VALUES' + sLineBreak +
                                   '( :CN, :FANTASIA, :RAZAO)';
var
  FDInsertForn : TFDQuery;
begin
  FDInsertForn := TFDQuery.Create(nil);
  FDInsertForn.Connection:= UDM.FDConexao;
  FDInsertForn.SQL.Clear;
  FDInsertForn.SQL.Add(SQLInsert);
  FDInsertForn.Params.ParamByName('CN').AsString:= dado;
  FDInsertForn.Params.ParamByName('FANTASIA').AsString:= EdFantasia.Text;
  FDInsertForn.Params.ParamByName('RAZAO').AsString:= EdRazao.Text;
  FDInsertForn.ExecSQL; 
end;

But in this way there is still a problem, because we are creating an object and we are not destroying it, ideally doing it with try and finally

procedure TFLancamento.EdRazaoExit(Sender: TObject);
const
  SQLInsert : String =  'INSERT INTO FOR1A' + sLineBreak +
                                   '( CNPJ,FANTASIA,RAZAO)VALUES' + sLineBreak +
                                   '( :CN, :FANTASIA, :RAZAO)';
var
  FDInsertForn : TFDQuery;
begin
  FDInsertForn := TFDQuery.Create(nil);
  try
    FDInsertForn.Connection:= UDM.FDConexao;
    FDInsertForn.SQL.Clear;
    FDInsertForn.SQL.Add(SQLInsert);
    FDInsertForn.Params.ParamByName('CN').AsString:= dado;
    FDInsertForn.Params.ParamByName('FANTASIA').AsString:= EdFantasia.Text;
    FDInsertForn.Params.ParamByName('RAZAO').AsString:= EdRazao.Text;
    FDInsertForn.ExecSQL;
  finally
    FDInsertForn.Free;
  end;
end;

In this way we made the top, the object will be instantiated in the first line. If during the process of block try an error occurs or if everything works out, finally will be executed and then the object will be destroyed.

    
16.12.2018 / 14:33