Solved

Exporting CSV file using VB 6

Posted on 2010-09-09
10
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

724 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