export to a csv file

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.
 
P

Peter Hoyle

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top