HOW TO READ DATA FROM A CSV FILE USING VISUAL BASIC 2008 (VB 2008)
DESCRIPTION
This free Visual Basic 2008 VB2008 code is a simple source code example of
reading data from a CSV
file using Visual
Basic 2008.
|
THE VISUAL BASIC 2008 VB2008 SETUP
Add the following lines to your 'Module1' code (add the Imports as the first
lines above Module1 - ie at the top!)
Imports System
Imports System.IO
Imports System.Collections
Module1:
Public MyCSVReader As System.IO.StreamReader
Public Line_Count As Integer
Public MyData1(1000) As String
Public MyData2(1000) As String
Public MyData2(1000) As String
|
THE FREE VISUAL BASIC 2008 VB 2008 SOURCE CODE
Copy and Paste this Visual Basic 2008 code wherever you need it:
'.....Free Source Code from www.freevisualbasic2008sourcecode.com
'.....Define a StreamReader that we can use to read the CSV file
'.....Note that MyFileName should include the path to be safe
MyFileName="c:\myfile.csv"
MyCSVReader = My.Computer.FileSystem.OpenTextFileReader(MyFileName)
Line_Count = -1 '.....Start the count at -1 so the first record is 0
Do While Not MyCSVReader.EndOfStream '.....Repeat until no more data
Line_Count = Line_Count + 1 '.....Increment the record counter
'.....Define an array created from the CSVReader data, splitting up the line at
each comma - NOTE any character can be used if desired not just a comma. But it
must match the file format used!
Dim MyFieldInput() As String = Split(MyCSVReader.ReadLine, ",")
MyData1(Line_Count) = MyFieldInput(0) '.....Assign the first data
MyData2(Line_Count) = MyFieldInput(1) '.....Assign the second data
MyData3(Line_Count) = MyFieldInput(2) '.....Assign the third data
Loop
MyCSVReader.Close() '.....Close the StreamReader
|
|