Read CSV data?

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:-

  1. How do I fix the error?
  2. 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.
  • How to fix the error.

Thanks again.

Where in the documentation did you find that code? This line

f = GetFolderItem("text/plain") //defined as a FileType

Should be

 f = GetOpenFolderItem("text/plain") //defined as a FileType

It won’t matter where you put the file, because that opens a file selection dialog.

And do you have a listbox named ListBox1?

Thanks, Tim. “GetOpenFolderItem” gives me an error when I try to compile as well.

Did you add the FileTypeSet? What is the error?

The error is “This item does not exist.”

Do you have a listbox named “Listbox1”?

Oh! I just reread where you’re using a WEB app. That code example doesn’t apply. It’s for Desktop only.

Is there an alternative for web apps? Or am I better off using MySql?

You can use GetFolderItem to access a file on the server. Put the file next to the WE executable and use

dim f as FolderItem = GetFolderItem("filename.ext")

using your actual file name, of course.

Beyond that, what is your setup? If you have a WebListBox on your webpage, you can pretty much substitute it for ListBox1 in the example.