I can not write data to a txt file

-1

Code:

qr.ParamByName('mes').AsString:= edmes.Text;
qr.ParamByName('ano').AsString:= edano.Text;
qr.Active:= true;
qr.First;
try
  AssignFile(arq, 'relatorio.txt');
  Rewrite(arq);

  showmessage(inttostr(qr.RecordCount));

  while not qr.Eof do
  begin
    writeln(arq, qr.FieldValues['dt_ficha'] + ';' + qr.FieldValues['unidade'] + ';' + qr.FieldValues['profissional'] + ';' + qr.FieldValues['count']);
    qr.Next;
  end;

  CloseFile(arq);
  ShowMessage('Arquivo criado com sucesso!');
except
end;

The qr component runs correctly, showmessage informs me that it has 270 rows, but the while statement is only executed once (one line only).

    
asked by anonymous 20.08.2018 / 14:35

1 answer

1

Try this way

qr.ParamByName('mes').AsString:= edmes.Text;
qr.ParamByName('ano').AsString:= edano.Text;
qr.Open;

try
  AssignFile(arq, 'relatorio.txt');
  Rewrite(arq);

  showmessage(inttostr(qr.RecordCount));
  qr.First;
  while(not(qr.Eof))do
  begin
    writeln(arq, DateTimeToStr(qr.FieldValues['dt_ficha']) + ';' + qr.FieldValues['unidade'] + ';' + qr.FieldValues['profissional'] + ';' + IntToStr(qr.FieldValues['count']));
    qr.Next;
  end;

  CloseFile(arq);
  ShowMessage('Arquivo criado com sucesso!');
except
  On E: Exception do
    ShowMessage(E.message);
end;
    
20.08.2018 / 14:43