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()
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 conversation is currently closed to new comments.
You could use an array but it would be PIA. Define a class corresponding to the record (or even just the fields you want). Then a collection of them. That will cope with the variable number of records nicely, and easy to enhance. The For each isn't really helping you out either with this idea and you'd be better off identifying them by index. The class wizard in the IDE will whop up most of this for you. You can even stick a LoadFromFile method on the collection and keep the whole thing nicely encapsulated. In fact you can do a SaveToFile as well.
There are some potential generic solutions as well, though unless you've some use for it, probably not worth the effort.
The generic idea would require unique column names ,preferably in the file. Then define a collection, add a field value to it with id of columnname. Then have a class that only holds this collection which becomes your record. Then define a collection of the 'record' class which becomes the file. You can access it in all sorts of ways then especially if each record has a unique identifier.
I have managed to do this by uploading each line to oracle as soon as the variables in the sql statement are initialised. Looping through until all lines in the csv are finished. Works well apart from... See top comment.
If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.
Visual Basic CSV Reader
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??