Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

writing a text file with the content of a table

Posted on 2006-11-23
4
Medium Priority
?
162 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 80 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 320 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

916 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