Solved

Need help creating a .csv file from my VB6 application

Posted on 2013-12-23
6
907 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

860 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