Solved

Exporting CSV file using VB 6

Posted on 2010-09-09
10
395 Views
Last Modified: 2013-12-26
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?
0
Comment
Question by:DistillingExperts
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:bureshd
ID: 33636866
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
0
 

Author Comment

by:DistillingExperts
ID: 33637094
As I said, "I only understood the 'for' part". Do you know what the others commands are doing?
0
 
LVL 12

Expert Comment

by:GMGenius
ID: 33637210
I have just reviewd the code on the forum and to me it will only export the column headers and 1 row of data
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:bureshd
ID: 33637248
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.
0
 

Author Comment

by:DistillingExperts
ID: 33637329
The points are yours. However, do you know a better way to export a recordset to a csv file?
0
 
LVL 12

Expert Comment

by:GMGenius
ID: 33637339
Ignore my last post but to explain how getstring works
http://msdn.microsoft.com/en-us/library/ms676975(v=VS.85).aspx
 
0
 
LVL 2

Accepted Solution

by:
bureshd earned 500 total points
ID: 33637535
There isn't a built-in function that allows you to export a recordset to a csv so you must iterate through the recordset and keep appending to a string or do as they did on that forum and use GetString. I think the way they did it will be the most efficient. Thanks
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 33637622

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
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 33637642
Wow, look at al the posts done while I was typing an answer. Better take typing classes...LOL
0
 

Author Comment

by:DistillingExperts
ID: 33637937
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question