Visual Basic CSV Reader - TechRepublic
General discussion
January 11, 2006 at 12:44 PM
frenchwood

Visual Basic CSV Reader

by frenchwood . Updated 20 years, 5 months ago

I am trying to read a CSV file usng VB.

I have the current code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStream As IO.Stream
Dim openFileDialog1 As New OpenFileDialog()
Dim CurrentField As String
Dim CurrentRow As String()

openFileDialog1.InitialDirectory = “c:\”
openFileDialog1.Filter = “Comma Seperated files (*.csv)|*.csv” ‘|All files (*.*)|*.*
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(myStream)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(“,”)

While Not MyReader.EndOfData
Try
CurrentRow = MyReader.ReadFields()
For Each CurrentField In CurrentRow
MsgBox(“‘” & CurrentField & “‘,”) ‘ This is temporary to test fields
Next
Catch ex As _
Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox(“Line ” & ex.Message & _
“is not valid and will be skipped.”)
End Try
End While
End Using
myStream.Close()
End If
End If
End Sub

What i need is to find 3 different columns, and then select values from these 3 columns based on data in a forth. Ideas anyone??

This discussion is locked

All Comments