Create a CSV File with VB

Hi friends,

This is a method to create a CSV file with VB6.
To make it with vb2005 just upgrade the code with the vb2005 tool

The Code :

Dim nfile As Integer
Dim conx as Integer

Private Sub cmdExport_Click()

nfile = FreeFile
ProgressBar1.Value = 1
Open "filea" For Output As #nfile

For conx = 1 To nbr
StatusBar1.Panels(1).Text = CInt(conx * 100 / nbr) & "% completed"

'Print to file each field of the structure "Customer"
Print #nfile, conx & ", " & Customer(conx).Field1 & ", " & Customer(conx).Field2 & ", " & Customer(conx).Field3 & ", " & Customer(conx).Field4 & ", " & Customer(conx).Field5 & ", " & Customer(conx).Field6 & ", "
ProgressBar1.Value = conx * 100 / nbr

Next conx

Close #nfile
FileCopy "filea", App.Path & "\ExportedFile.csv"
Kill "filea"

If err.Number = 0 Then

MsgBox "File exported to working directory!"
Else
MsgBox "Error exporting file: " & err.Number
End If
End Sub

'The data structure has to be defined like this one:
Type Customer

Field1 as String
Field2 as String

'You can add as many fields as you need
...
End Type


Hope it helps and good luck, any suggestion or questions post a comment !

0 comments:

Post a Comment