Solved

Need help creating a .csv file from my VB6 application

Posted on 2013-12-23
6
940 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

705 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