-
Jul 31st, 2007, 03:31 PM
#1
VB6 - Save Recordset to CSV format
This is a function I made that is quite usefull when you want to save the data in a Recordset directly to a CSV file.
Code:
Public Function RecordsetToCSV(rsData As ADODB.Recordset, Optional ShowColumnNames As Boolean = True, Optional NULLStr As String = "") As String
Dim K As Long, RetStr As String
If ShowColumnNames Then
For K = 0 To rsData.Fields.Count - 1
RetStr = RetStr & ",""" & rsData.Fields(K).Name & """"
Next K
RetStr = Mid(RetStr, 2) & vbNewLine
End If
RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
RetStr = Left(RetStr, Len(RetStr) - 3)
RecordsetToCSV = RetStr
End Function
This function returns a string in CSV format, to save to a file, you should do something like:
Code:
Dim CSVData As String
CSVData = RecordsetToCSV(rsData, True)
Open "C:\test.csv" For Binary Access Write As #1
Put #1, , CSVData
Close #1
-
Jan 8th, 2009, 02:24 PM
#2
Member
Re: VB6 - Save Recordset to CSV format
Thanks... very helpful!!!!
-
Jan 10th, 2009, 06:23 PM
#3
Re: VB6 - Save Recordset to CSV format
Of course it destroys any type information of the fields, saving everything as text. It also fails to handle quoting quotes that occur in fields, producing a broken CSV file in such a case.
It is short and sweet though!
-
Feb 24th, 2022, 02:36 PM
#4
New Member
Re: VB6 - Save Recordset to CSV format
This code snippet is very usefull.
As @dilettante said, this fails to handle whenever a double quotes is present in the data, any idea to fix this will be really helpful.
thanks in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|