Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2008, 04:02 AM
Registered User
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exporting a csv file (comma separated)

Good day to all!

Got another question to post here, I hope the senior and advance member here will read this. How can I export the data that I showed in my Datagrid as csv file (comma separated). This will help me to show large number of data to view excel or notepad.

Best Regards,
Micharl


 
Old May 5th, 2008, 12:06 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I don't know that the datagrid can be used this way, but exporting these data is not hard.

Open a recordset in code, open a file to receive the output as write (not append), then, in a loop (Do Until rs.EOF = True / Loop) construct a line to go the the file in a string variable, the write it out to the file.

It's not much code to write, you will have intimate control over the formatting of the outpu, it will run fast, and so on.
 
Old May 7th, 2008, 04:27 AM
Registered User
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian, is it ok to give me an example on how to do this? I'm trying to get the data from the database server and show it to datagrid. After that, the output data should be save as .csv file. Thank you so much in advance. I hope you can give time to this.

Quote:
quote:Originally posted by BrianWren
 I don't know that the datagrid can be used this way, but exporting these data is not hard.

Open a recordset in code, open a file to receive the output as write (not append), then, in a loop (Do Until rs.EOF = True / Loop) construct a line to go the the file in a string variable, the write it out to the file.

It's not much code to write, you will have intimate control over the formatting of the outpu, it will run fast, and so on.
 
Old May 7th, 2008, 08:50 AM
Authorized User
 
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
Default

hi

First load all data to a collection something like

dim col as new collection()

loop thro the source(recordset or dataset) and add the comma seperated string to

col.add(rs(1) & "," rs(2)....)

then pass the collection to the function (can be taken from here)
http://www.vbknowledgebase.com/WebAp...n-To-Text-File


in the function if you dont want to have the overwrite functionality remove this line
If Not IsOverWrite Then strContent = Text2String(strPath)

Col2Text col,"c:\myexport.csv",0

if you need further assistance please dont hesitate



Pon Saravanan
 
Old May 7th, 2008, 02:32 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Add the Microsoft Windows Scripting Host to your project as a reference. It make working with files easier.

Add a reference to Microsoft ActiveX Data Objects

Create a connection to your data. I am using Oracle, so for me it is:
Code:
    Dim c    As ADODB.Connection
    Set c = New ADODB.Connection

    c.Provider = "MSDASQL"              ' OLE-DB Provider for ODBC.
                                        ' My DB is named DIS...
    c.ConnectionString = "DRIVER={Microsoft ODBC for Oracle};" & _
                         "SERVER=DIS;" & _
                         "UID=USERNAME;" & _
                         "PASSWORD=SecrEt$tring;"
    c.ConnectionTimeout = 10
    c.Open
    Then you use the connection to open a recordset:
Code:
    Dim r As ADODB.Recordset
Code:
    r.Open "SELECT FIELD1, DATASTUFF, DATES_OF_FUN " & _
           "FROM   THE_TABLE_NAME " & _
           "WHERE  DATES_OF_FUN = " Now(), _
           c
           Open the output file:
Code:
    Dim fso As New FileSyTemObject
Code:
    Dim ts  As TextStream
    Set ts = FSO.CreateTextFile("C:\Temp\OutPut\TheData.csv", False, False)
    Now you’re ready to spit out the data:
Code:
    Do Until r.EOF
Code:
        ts.WriteLine """" & r!FIELD1 & """," & _
                            r!DATASTUFF & "," & _
                            r!DATES_OF_FUN 
        r.MoveNext
    Loop
    When you are all done:
Code:
    ts.Close
Code:
    Set ts = Nothing
    Set fso = Nothing
    r.close
    Set r = Nothing
    c.close
    Set c = Nothing
 
Old May 7th, 2008, 02:35 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

By the way, introducing a Collection to this process will slow your code down, and will not provide any real benefit. If, at the point Pon Saravanan suggested adding the data to the Collection you add it to the file instead, then you are done—without having to further process the Collection.
 
Old May 7th, 2008, 10:10 PM
Registered User
 
Join Date: May 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it! Thank you so much Pon & Brian, I combine the idea of the examples that you gave me. Again, thank you so much!!!

Best Regards,

Micharl






Similar Threads
Thread Thread Starter Forum Replies Last Post
Comma Separated values aldwinenriquez SQL Server 2005 3 June 1st, 2007 08:52 PM
comma separated list MunishBhatia SQL Server 2000 11 March 21st, 2007 03:56 PM
CSV File to Array Problems with Comma in Field MSchoaf Classic ASP Basics 1 July 19th, 2006 05:58 PM
Import a Comma separated txt file to SQL table arielote C# 2 April 17th, 2006 01:08 AM
Select from Comma Separated Values sasidhar79 SQL Server 2000 2 March 28th, 2005 06:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.