Solved

Need help creating a .csv file from my VB6 application

Posted on 2013-12-23
6
921 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
[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
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
Industry Leaders: 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!

 
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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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