FDQUERY at runtime

Asked

Viewed 1,711 times

1

I’m trying to use the FDQuery only at runtime. I’ve done a lot of research and tried several changes, but they all end with Access Violetion, so it’s suspicious that the component isn’t being instantiated properly. I looked for examples on the Internet to study and understand my mistake, and until now I found nothing I haven’t done.

I leave here my example of how the code is now, and please someone 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;
  • In which line does the error? What is the error?

  • Error from the first line. I got to comment on Params and do a Debug with Break Point but as soon as the first line starts comes access violetion.

  • 1

    So probably your FDQuery not created, try adding this line FDInsertForn := TFDQuery.Create(nil); before FDInsertForn.Connection:= UDM.FDConexao;

  • The problem was solved, I looked for this example you gave me and even in the official documentation of Embarcadeiro has this quote

  • I added the answer, with a few more details as well.

1 answer

2


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

FDInsertForn := TFDQuery.Create(nil);

But in this code there are other points to be commented, as you are creating your TFDQuery at runtime, it is not necessary to declare it in 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;

However, in this way there is still a problem, because we are creating an object and we are not destroying, the ideal to do this with the 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;

This way we did the top, the object will be instantiated in the first line. If during the block process try an error occurs or if everything goes right, the block finally will be executed and then the object will be destroyed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.