schneie1974
New member
- Local time
- Today, 15:45
- Joined
- Jun 22, 2015
- Messages
- 6
Hello,
I did some basic VBA programming some 10 years back and do now need to solve just 1 problem.
I have the requirement to write EACH record from a table to its own CSV file with name of the file beeing combination of 2 fields
So let's say I have Table1 with 3 columns (Field1, Field2, Field3) with following content :
Field1 Field2 Field3
AA 1 ABC
AA 2 DEF
AA 3 GHI
I should get 3 files with names AA1.CSV, AA2.CSV and AA3.CSV and each file contains its respective row from the table.
I tried to do it with DAO Recordset, but I do not find a way to write only the current record from recordset while looping.
See below the code I was using, but issue is that code does succesfull creates the 3 CSV files as per above example, but in each file it writes ALL 3 ROWS instead only the respective ROW.
Hope you can help me here, maybe I need to approach totally different.
I did some basic VBA programming some 10 years back and do now need to solve just 1 problem.
I have the requirement to write EACH record from a table to its own CSV file with name of the file beeing combination of 2 fields
So let's say I have Table1 with 3 columns (Field1, Field2, Field3) with following content :
Field1 Field2 Field3
AA 1 ABC
AA 2 DEF
AA 3 GHI
I should get 3 files with names AA1.CSV, AA2.CSV and AA3.CSV and each file contains its respective row from the table.
I tried to do it with DAO Recordset, but I do not find a way to write only the current record from recordset while looping.
See below the code I was using, but issue is that code does succesfull creates the 3 CSV files as per above example, but in each file it writes ALL 3 ROWS instead only the respective ROW.
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strFilename As String
strSQL = "SELECT Field1, Field2 FROM Table1"
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset(strSQL)
If Not ((rs.EOF) And (rs.BOF)) Then
rs.MoveFirst
strSQL = ""
Do While Not (rs.EOF)
strSQL = "SELECT * from Table1"
strFilename = "c:\temp\" & rs!Field1 & "_" & rs!Field2 & ".csv"
DoCmd.TransferText acExportFixed, "EXPORT2", Table1, strFilename
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
MsgBox "Exporting Done.." & vbCrLf & "Dir.. " & Left(strFilename, 13)
Hope you can help me here, maybe I need to approach totally different.
Last edited: