Copy Records to a Text File : Recordset Save « Access « VBA / Excel / Access / Word






Copy Records to a Text File

 
Sub WriteToFile()
   Dim conn As ADODB.Connection
   Dim myRecordset As ADODB.Recordset
   Dim f As ADODB.Field
   Dim myFileSystemObject As Object
   Dim txtfile As Object

   Set conn = New ADODB.Connection
   conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & "\mydb.mdb"

   Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
   Set txtfile = myFileSystemObject.CreateTextFile("c:\testfile.txt", True)

   Set myRecordset = New ADODB.Recordset
   myRecordset.Open "[Order Details]", conn
   With myRecordset
      For Each f In .Fields
         txtfile.Write (f.Name)
         txtfile.Write Chr(9)
      Next
      txtfile.WriteLine
      txtfile.Write myRecordset.GetString(adClipString)
      .Close
   End With

   txtfile.Close
   Set myRecordset = Nothing
   conn.Close
   Set conn = Nothing
End Sub

 








Related examples in the same category

1.Saving Records to a Disk File