Hi all. I’m a beginner and I need help writing code that allows me to parse CSV data for use in a WEB APP. I currently have the following code (taken from the documentation);-
[code] Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i As Integer
f = GetFolderItem(“text/plain”) //defined as a FileType
If f <> Nil And f.Exists Then
Dim tab As String = ChrB(9)
textInput = TextInputStream.Open(f)
While Not textInput.EOF
rowFromFile = textInput.ReadLine
// Set
If ListBox1.ColumnCount < CountFields(rowFromFile, tab) Then
ListBox1.ColumnCount = CountFields(rowFromFile, tab)
End If
ListBox1.AddRow(NthField(rowFromFile, tab, 1))
For i = 1 To CountFields(rowFromFile, tab)
oneCell = NthField(rowFromFile, tab, i)
ListBox1.Cell(ListBox1.ListCount-1, i-1) = oneCell
Next
Wend
textInput.Close
End If
[/code]
The code gives me the error : “This item does not exist…”
ListBox1.Cell(ListBox1.ListCount
I have 2 questions:-
How do I fix the error?
How do I alter the code to account for comma-delimited data?
Thanks in advance. If anyone has any code reads CSVs (comma-delimited) and is nice enough to share, I’d really appreciate that too.
It looks like your code is looking for tab-delimited data
if it is comma-seperated, you should be able to do
ListBox1.AddRow(split(rowFromFile,","))
and as long as the listbox has enough columns, it should fill it in. No need for the for next loop
As an aside, I would recommend using listbox1.lastindex rather than listbox1.listcount-1 (otherwise if you at some point decide to insert a row rather than add, you’ll be adding data to the wrong row)
Thanks for the reply, Jim. The code I posted is just a starting point. I’m going to alter it to use an array instead of a ListBox but that’s not the issue.
The error “This item does not exist… ListBox1.Cell(ListBox1.ListCount-1, i-1) = oneCell” still exists. I’m not even sure where to place the CSV file to use with the procedure either. Again, I’m a Xojo noob so please forgive my ignorance.
I need to know:-
Where to place the CSV file or even how to refer to it in the code.