Solved

writing a text file with the content of a table

Posted on 2006-11-23
4
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run video youtube webbrowse 10 86
How to renew a Delphi rad-studio licence? 5 95
Convert a string into a TDateTime 5 80
tvirtualstringtree freeze when load too manny images 10 94
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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