Solved

writing a text file with the content of a table

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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