export to a csv file

  • Thread starter Thread starter JIM.H.
  • Start date Start date
J

JIM.H.

Hello,
I have a table: table:id,name. Now I need to export this
table to many id.csv files. All names for the same id goes
to id.csv, and this happens for all id in the table.
How can I do that?
Thanks,
Jim.
 
Hi Jim,

Possible code for adaptation below,

Sub CreateFile()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim fso As Object, tf As Object
Dim strSQL As String
Dim strFileName As String
Dim lngID As Long
Dim lngIDTemp As Long

Set dbs = CurrentDb
Set fso = CreateObject("Scripting.FileSystemObject")
strSQL = "SELECT ID, Name " _
& "FROM MyTable ORDER BY ID, Name"
Set rst = dbs.OpenRecordset(strSQL)
rst.MoveFirst
Do Until rst.EOF
lngID = rst!ID
lngIDTemp = lngID
strFileName = rst!ID & ".csv"
Set tf = fso.CreateTextFile("c:\" & strFileName, True)
Do Until lngID <> lngIDTemp
tf.WriteLine rst!ID & ";" & rst!Name
rst.MoveNext
If rst.EOF Then
Exit Do
End If
lngID = rst!ID
Loop
tf.Close
Loop

End Sub

Cheers,
Peter
 
Back
Top