Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting listbox fields to csv file

Status
Not open for further replies.

SHAWTY721

Programmer
Aug 16, 2007
116
US
I am working on a VB6 project and I am trying to export fields from a listbox into a csv file. I can get the titles to export but none of the actual content from the listbox is being exported to the newly created csv file. Is there something missing in my code that can is suppose to be there to export the content.

Code:
Private Sub btnExport_Click()

'This subroutine exports the list to a CSV file

Dim OpenFile As Integer
Dim Count As Integer
Dim Element As Integer
Dim FilePath As String
Dim SamplePlant() As String
Dim SampleBatch() As String
Dim SampleStage() As String
Dim SampleTime() As String
Dim SampleAcidNumber() As String
Dim SampleViscosity() As String
Dim SampleTemp() As String
Dim SampleFlow() As String
Dim SampleANDiff() As String
Dim SamplePredAdd() As String
Dim SamplePredAmt() As String
Dim SampleActAdd() As String
Dim SampleActAmt() As String

FilePath = App.Path & "\Exported\"

glbExportPath = FilePath

'Write to file
OpenFile = FreeFile

Open glbExportPath & "Recommended Addition(" & lblProduct(16).Caption & ").csv" For Output As OpenFile

'Header
Print #OpenFile, "Plant, Batch, Stage, Time, Acid Number, Viscosity, Temp, Flow, Acid Number Difference, Predicted Add, Predicted Amount, Actual Add, Actual Amount"

'Body

For Count = 1 To Element
    Print #OpenFile, SamplePlant(Count) & ", " & SampleBatch(Count) & ", " & SampleStage(Count) & ", " & SampleTime(Count) & ", " & SampleAcidNumber(Count) & ", " & SampleViscosity(Count) & ", " & SampleTemp(Count) & ", " & SampleFlow(Count) & ", " & SampleANDiff(Count) & ", " & SamplePredAdd(Count) & ", " & SamplePredAmt(Count) & ", " & SampleActAdd(Count) & ", " & SampleActAmt(Count); ""
Next Count

Count = Count + 1

Close OpenFile

MsgBox "The data has been exported. The following file was created: " & glbExportPath & "Recommended Addition ( " & lblProduct(16).Caption & ").csv", vbExclamation, "File Done"

End Sub
 
>For Count = 1 To Element
You have not initialized Element so it equals 0.
The for loop is therefor not even entered.
 
You need to set Element to the number of items there is in the listboxes/arrays you want to export.
Unless you have "Option Base" set to 1 then you probably want it to say "For Count=0 to Element"
Also, I can't see anywhere where you have put any data into the arrays you define in the beginning of the sub and then use in the for loop.
If you have all the data in listboxes and you have the same number of items in each listbox you could replace "Elements" with "Listboxname.Count-1" and refer to the listboxes instead of the string arrays.
 
Code:
For Count = 0 to ListBox.ListCount - 1
    .....
Next Count

HTH

Chew



10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top