Link to home
Start Free TrialLog in
Avatar of DistillingExperts
DistillingExperts

asked on

Exporting CSV file using VB 6

Hi,

I am using the below link in order to generate a file but while it works, I don't undertand why:
http://www.vbforums.com/showthread.php?t=481705

I only understood the for part. The others parameters, I have no clue.

Could you please give me a hand on the meaning for those commands?
Avatar of bureshd
bureshd

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

This piece is basically appending the column titles as the first line if the ShowColumnNames parameter is set to true. The "For K = 0 To rsData.Fields.Count - 1" is just iterating through all the columns and appending the values to the "RetStr" string variable which is what is compiled through the whole function.

Once they have completed the column titles, it goes through and appends the rest of the data line by line. Hope this helps. Thanks
Avatar of DistillingExperts

ASKER

As I said, "I only understood the 'for' part". Do you know what the others commands are doing?
Avatar of GMGenius
I have just reviewd the code on the forum and to me it will only export the column headers and 1 row of data
RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)
    RetStr = Left(RetStr, Len(RetStr) - 3)

These lines of code are just appending the rows in the dataset in csv format.
String = recordset.GetString([StringFormat], [NumRows], [ColumnDelimiter], [RowDelimiter], [NullExpr])
String = Left([StringFormat], [Int Length])

The for statement is just for the column titles while the rest of the function appends the recordset to the string that it passes back.
The points are yours. However, do you know a better way to export a recordset to a csv file?
Ignore my last post but to explain how getstring works
http://msdn.microsoft.com/en-us/library/ms676975(v=VS.85).aspx
 
ASKER CERTIFIED SOLUTION
Avatar of bureshd
bureshd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Dim K As Long, RetStr As String <--- DECLARES VARIABLES USED
   
If ShowColumnNames Then <---IF YOU WANT A HEADER AS THE 1ST LINE, THE FIELD NAMES ARE USED
    For K = 0 To rsData.Fields.Count - 1 <---CYCLES THRU FIELDS
        RetStr = RetStr & ",""" & rsData.Fields(K).Name & """" <--- BUILDS CSV LINE OF FIELD NAMES
    Next K
     
    RetStr = Mid(RetStr, 2) & vbNewLine <--- ADDS CARRIAGE RETURN TO END OF LINE
End If
   
RetStr = RetStr & """" & rsData.GetString(adClipString, -1, """,""", """" & vbNewLine & """", NULLStr)

GETSTRING PARAMETERS:
GetString([StringFormat], [NumRows], [ColumnDelimiter], [RowDelimiter], [NullExpr])

SO, THIS COMMAND IS BUILDING YOUR CSV DATA LINE

RetStr = Left(RetStr, Len(RetStr) - 3) <--- CHOPS OFF TRAILING CHARS YOU DON'T NEED
   
RecordsetToCSV = RetStr <--- TELLS FUNCTION TO RETURN THE BUILT CSV STRING
Wow, look at al the posts done while I was typing an answer. Better take typing classes...LOL
VBClassicGuy, if that makes you feel better, I actually got rid of my problem with you solution. Thank you very much indeed. Buresd arrived very close, but your was the best one.