Solved

Exporting CSV file using VB 6

Posted on 2010-09-09
10
397 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to mongodb 2 138
SLMGR Switches Are Not Working On KMS Host 3 137
backup program with robocopy 6 69
checkbox to hide entire section 10 58
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 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