Output to text file

Directlinq

Registered User.
Local time
Today, 08:10
Joined
Sep 13, 2009
Messages
67
Could somebody please help me write a code for creating 1 .txt file for every record in a table.
Each filename will be field1 & .txt from the table
and on each line inside the text file would be field2, next line field3 etc..
I also need to be able to put some writing before and after the fields inside the text file e.g.

<<field2>>
<test<field3>::::

Ive been tring for hours and cant find any code to help me.
Many Thanks
 
You need a TextStream object that you can get from a FileSystemObject.GetTextFile() method. I think.
Set a reference to Microsoft Scripting Runtime and use code like...
Code:
dim fso as new scripting.filesystemobject
dim ts as scripting.textstream

set ts = fso.gettextfile("somefilepath", forWriting)
ts.write "write something now "
ts.writeln "write again"
ts.close
There will be errors in that code...
Cheers,
 
Try this.

Code:
Dim sFile As String
Dim i As Integer
Fim ff As Long

Dim Rs As DAO.Recordset
Set Rs = CurrentDb.OpenRecordset(<<YourTableNameHere>>)

If Not Rs.EOF And Not Rs.BOF Then
  
   Do Until Rs.EOF
       If Not IsNull(Rs(0)) Then
          ff = FreeFile
          sFile = "<<Path>>" & "\" & Rs(0) & ".Txt"
          Open sFile For Output As #ff
          Print #ff, "<<Your header text here>>" & vbNewLine
          For i = 0 To Rs.Fields.Count
               Print #ff, Rs(i).Name & ":" & Nz(Rs(i),"Null") & vbNewLine
          Next i
         Print #ff, "<<Your Footer Text Here>>"
         Close #ff
     End If
   Rs.MoveNext
   Loop
   Rs.Close
End If
Set Rs = Nothing

Why would you want to do this anyway?
What happens if you have 10,000 records in your table? you will get 10,000 txt files.
What is in the first field that you are using as part of your naming convention?
What happens if you have duplicates?

David
 

Users who are viewing this thread

Back
Top Bottom