Solved

writing a text file with the content of a table

Posted on 2006-11-23
4
155 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need Delphi function to get Youtube video title 5 225
indy tidtcpclient threading issue 8 63
code issue 8 99
How to load 2 images in same column in Delphi 2 32
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now