Just over a year ago I made a program that would load a SQLQuery into a DBGrid and then using code I found here, I would export it to CSV.
procedure TForm1.ExportLogs(filename : string);
var
I, J: Integer;
SL: TStringList;
begin
SL := TStringList.Create;
SQLQuery.First;
SL.Add('');
for I := 0 to DBGrid1.Columns.Count -1 do
begin
SL[0] := SL[0] + (DBGrid1.Columns[I].Title.Caption) + ',';
end;
for I := 1 to SQLQuery.RecordCount do
begin
SL.Add('');
SQLQuery.RecNo := I;
for J := 0 to SQLQuery.Fields.Count - 1 do
SL[SL.Count - 1] := SL[SL.Count - 1] + SQLQuery.Fields[J].AsString + ',';
end;
SL.SaveToFile(filename);
SL.Free;
ShowMessage('Log results exported to /' + filename);
end;
I swear this used to work perfectly, it would export just as it looked in the DBGrid.
Now, when I export, a few of the columns don't match the fields, it's not like it is in the DBGrid...They are re-ordered in the SQLQuery.
Problem is, I'm not sure how to go about fixing this or re-ordering a query's results. Any ideas?