Solved

Need help creating a .csv file from my VB6 application

Posted on 2013-12-23
6
868 Views
Last Modified: 2014-01-08
Hi Experts,
I have an array, in my VB6 app, that I fill with records from a jBase database.  The array holds close to 100,000 records.  What is the easiest way to place the records in the array to a .CSV file?  


Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
6 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39736422
Put the JBase records in a datatable and later export the datatable to the CSV like this

 Public ExportToCSV(dt as Datatable)
    Dim StrFilePath as string="c:\csvData.csv"
   Dim sw as StreamWriter=new StreamWriter(strFilePath,false)
   For Each rw as Datarow in dt.Rows
     sw.Write(rw("FieldName").ToString)
     sw.Write(sw.NewLine)
   Next
 End Sub

Open in new window

0
 

Author Comment

by:mainrotor
ID: 39738101
How does one use StreamWriter in VB6?  I keep getting an error.  Do I need to reference something before using it?

Thanks in advance,
mrotor
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39738219
Import System.IO (Reference this class)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:aikimark
ID: 39739344
I assume that you have a 2D array.

Iterate the rows and write each row to a file.
With each row iteration, iterate the columns to a string variable.

Note: You can either use string concatenation or copy the column data to a 1D array (vector) data structure and then use the JOIN() function to create the delimited string for your output operation.
0
 
LVL 2

Expert Comment

by:DiscGolfDad
ID: 39739359
This is just "air code" so I'm not certain of the UBound and LBound parameters, but I'm fairly sure the syntax is correct.

Dim intRow As Integer
Dim intCol As Integer
Dim intFileCh As Integer

intFileCh = Freefile

Open "Textfile.csv" For Output As intFileCh

For intRow = LBound(RecArray(1)) To UBound(RecArray(1))
  For intCol = LBound(RecArray(2)) To UBOUND(RecArray(2))
    If intCol > LBound(RecArray(2)) Then
      Print #intFileCh, ",";
    End If
    Print #intFileCh, Chr(34); RecArray(intRow, intCol); Chr(34);
  Next IntCol
  Print #intFileCh,""
Next intRow

Close intFileCh

Open in new window

0
 
LVL 3

Accepted Solution

by:
CvD earned 500 total points
ID: 39740688
If you fill an array from jBase, why not walk the resultset instead of filling an array first.

Suppose your recordset is called oRset and it contains rows with 2 fields "ID" and "NAME", then an answer could be something in the line of:

Public Sub exportMe(ByVal oRset As Variant, ByVal fileName As String)
  Dim ff As Variant
 
  ff = FreeFile
  Open fileName For Output As ff
  With oRset
    Do While Not .EOF And Not .BOF
      Write #ff, IIf(VarType(.fields("ID")) = 8, Chr(34) & .fields("ID") & Chr(34), .fields("ID")), Chr(34) & .fields("NAME") & Chr(34)
      .MoveNext
    Loop
  End With
  Close #ff
End Sub

This worked for me in a project where I had to query an oracle database and export the resulting recordset. Hope it works for you as well. Depending on the flavour of VB you're using you might give the parameter oRset another datatype (probably Object or RecordSet). In my situation Variant was sufficient.

Since I currently have no environment available to test it I sadly didn't. But since I used it in antorher project you should be fine with this code.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now