I am trying to copy data from tblDeelnemers
of dbDeelnemers
into textfile Deelnemers.txt
The table contains 4 columns namely: ID, Naam, van, kontaknommer. I have a TADOQuery on my form as well as a dbGrid. I am using Microsoft Access and Delphi 7.
I have search for possible ways but the only help i can get is for C#.
I Want to recall this Textfile later when a button is clicked. The format of the saved data doesn't matter I just need to be able to load it back into a dataset. I am doing it this way because after this "import" the data from the table is deleted to start from the beginning
I tried the following but it is completely wrong:
var
sDeelnemers : string;//string to copy data in before adding to textfile
Begin // sIDkies is global string
frmData.qryVGKompetisieDB.Active := false;
frmData.qryVGKompetisieDB.SQL.Text := 'copy ID, Naam, Van, Kontaknommer from tblDeelnemers into sDeelnemers WHERE ID = "'+sIDkies+'"; // copying data into string
frmData.qryVGKompetisieDB.ExecSQL;
frmData.qryVGKompetisieDB.SQL.Text := 'Select * from tblDeelnemers'; // to show table on dgGrid like originally
frmData.qryVGKompetisieDB.Active := true;
end; // then assigning textfile and copying sDeelnemers(string) into textfile
Thanks to @No'am Newman the code is now as followed:
procedure TfrmData.Button1Click(Sender: TObject);
var
f: tstrings;
q: TADOQuery;
begin
redInfo.Visible := false;
dbgInfo.Visible := true;
qryVGKompetisieDB.Active := false;
qryVGKompetisieDB.SQL.Text := 'select id, naam, van, Kontaknommer from tblDeelnemers';
qryVGKompetisieDB.ExecSQL;
qryVGKompetisieDB.Active := true;
f:= tstringlist.create;
with q do
begin
open; //Error occurs here
while not eof do
begin
f.add (fieldbyname ('id').asstring + ',' +
fieldbyname ('naam').asstring + ',' +
fieldbyname ('Van').asstring + ',' +
fieldbyname ('Kontaknommer').asstring);
next
end;
close
end;
f.savetofile ('Deelnemers.txt');
f.free
end;
But now I am getting the following error when the TADOQuery is opened (As indicated in the code):
Project PAT_p.exe raised exception class EAccessViolation with message 'Access Violation at address 004A990C in module 'PAT_p.exe'. Read of address 8BD88CCB'. Process stopped. Use Step or Run to continue.
You can iterate over the returned set and write each row into a text file. Your query appears to be
select id, naam, van, Kontaknommer
from tblDeelnemers
Here is some basic code to extract the values from all the rows in the table and write them to a text file (actually, a csv file - you can choose your separator).
var
f: tstrings;
q: tquery; // whichever type you use
begin
f:= tstringlist.create;
with q do
begin
open;
while not eof do
begin
f.add (fieldbyname ('id').asstring + ',' +
fieldbyname ('naam').asstring + ',' +
fieldbyname ('Van').asstring + ',' +
fieldbyname ('Kontaknommer').asstring);
next
end;
close
end;
f.savetofile ('Deelnemers.txt');
f.free
end;
It would be quicker to use permanent fields (ie qID.asstring, etc) rather than using 'fieldbyname' all the time. Also, you should put some error checking around the creation of the stringlist.