PDA

View Full Version : Exporting active record to csv format



ahsanriaz
12-14-2015, 09:56 AM
I need a code to export active record from split form to .csv format to specific location with save as value of specific field.

jonh
12-15-2015, 03:24 PM
For Each f In Me.Recordset.Fields
s = s & "," & f
Next

ff = FreeFile
Open "c:\file.csv" For Output As #ff
Print #ff, s
Close #ff

ahsanriaz
12-16-2015, 01:02 PM
Thanks Jonh for your reply, I tried that code but I am getting file/path error. I am attaching test database for your reference, can you please check. I want to export the current record in csv format and name as the value in order no field.

jonh
12-16-2015, 04:24 PM
Probably permissions. Change the path to a directory you can write to.

I fixed an error and added some error handling



Private Sub Transfer_Click()
On Error Resume Next
Const p As String = "C:\file.csv"
If Not killfile(p) Then Exit Sub

For Each f In Me.Recordset.Fields
If Len(s) Then s = s & ","
s = s & f
Next

ff = FreeFile
Open p For Output As #ff

Select Case Err.Number
Case 0
Case Else
MsgBox Err.Description
Exit Sub
End Select

Print #ff, s
Close #ff


End Sub


Private Function killfile(f As String) As Boolean
On Error Resume Next
Kill f
Select Case Err.Number
Case 0, 53
killfile = True
Case 70 'denied
Case Else
Debug.Print Err.Number, Err.Description
Stop
End Select
Err.Clear
End Function

ahsanriaz
12-17-2015, 04:27 AM
Thanks Jonh now its working fine but the problem is its not transferring the field name and other is every time its transferring the data in the same file. But I want every time when user click transfer its transferring the data along with field and the name of csv file is like the value in order no field like "152341001".