?
Solved

Exporting CSV file using VB 6

Posted on 2010-09-09
10
Medium Priority
?
402 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
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…
Suggested Courses

649 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