Solved

Need help creating a .csv file from my VB6 application

Posted on 2013-12-23
6
887 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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 …

816 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

8 Experts available now in Live!

Get 1:1 Help Now