Solved

writing a text file with the content of a table

Posted on 2006-11-23
4
156 Views
Last Modified: 2010-04-05
Hi guys,

Another simple question about msAccess... I am using ADO and have a ADOTable in a grid (the ADOTable has a lot of datafields)... I want to copy every register to a text file... Any ideas to do the job fast?

regards
Manuel Lopez (lopem)
0
Comment
Question by:lopem
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
bernani earned 20 total points
ID: 18004804

Hi,

Add a function like this in a module of your msAccess database

Sub SaveRegisterToFile
DoCmd.OutputTo acTable, "NameOfYourTableToSave", "MS-DOSText(*.txt)", "C:\Test-Table", True, ""
End Sub

And call this macro from your Delphi App  in a way like this:

var msApp : OleVariant;

msApp := GetActiveOleObject ( 'Access.Application' );
msApp.Run('SaveRegisterToFile');
msApp := Unassigned;
// test loading
if sysutils.filexists('c:\Test-Table.txt') then memo1.lines.loadfromfile('c:\Test-Table.txt');

See also in the msAccess vba help this: DoCmd.TransferText






0
 
LVL 14

Assisted Solution

by:Pierre Cornelius
Pierre Cornelius earned 80 total points
ID: 18008653
I wrote a function to export any descendant of TDataset as a comma-separated text file. If you don't want it as comma-separated, it is quite easy to adjust the code according to your needs. Here it is:

procedure SaveDatasetAsCSV(Dataset: TDataset);
var i: integer;
    F: TextFile;
    dlgSave: TSaveDialog;
begin
  if NOT Dataset.Active
    then raise TLDException.Create('Error saving dataset. Dataset is closed.');

  dlgSave:= TSaveDialog.Create(nil);
  dlgSave.Filter:= 'Comma separated text file (*.csv)|*.csv';
  dlgSave.FilterIndex:= 1;
  dlgSave.DefaultExt:= 'csv';
  dlgSave.Title:= 'Exporting dataset...';
  dlgSave.Options:= [ofOverwritePrompt, ofHideReadOnly, ofPathMustExist,
                     ofEnableSizing, ofDontAddToRecent];

  try
    Dataset.DisableControls;
    if dlgSave.Execute then
    begin
      AssignFile(F, dlgSave.FileName);
      Rewrite(f);
      for i:= 0 to Dataset.Fields.Count-1 do
        if i <> (DataSet.Fields.Count-1)
          then write(f, '"'+Dataset.Fields[i].FieldName+'",')
          else writeln(f,'"'+Dataset.Fields[i].FieldName+'"');

      Dataset.First;
      while not Dataset.Eof do
      begin
        for i:= 0 to Dataset.Fields.Count-1 do
          if i <> (Dataset.Fields.Count-1)
            then write(f, '"'+Dataset.Fields[i].DisplayText+'",')
            else writeln(f, '"'+Dataset.Fields[i].DisplayText+'"');
        Dataset.Next;
      end;
      Close(f);
    end;
  finally
    Dataset.EnableControls;
    dlgSave.Free;
  end;
end;


You would need to do something like
  SaveDatasetAsCSV(YourADO_Dataset);

Regards
Pierre
0
 
LVL 3

Author Comment

by:lopem
ID: 18012532
I prefer PierreC's answer because it is only a delphi solution and I don't have to dal with access in any circunstances. In any way, I want thanks both of you guys, to helped me in the solution of the problem

regards
Manuel Lopez (lopem)
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 18017148
Glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wininet read php file from internet issue 6 91
FMX enumerated colours 2 99
Print Graphic and Text to Epson TM-T88v 12 255
creating threads in delphi 1 104
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question