How to Read CSV File in VB

Written by jay rigsby
  • Share
  • Tweet
  • Share
  • Email

Comma separated value (CSV) is a simple text file format that is commonly used to store data. A row represents each record in this format, and within each row, a comma separates each value. Visual Basic 6 input-output file streams or data access object can be used to load and read a CSV file.

Skill level:

Things you need

  • Visual Basic 6

Show MoreHide


  1. 1

    Create a new project in Visual Basic. If you wish to add the functionality to read a CSV file to an existing project, you may skip this step.

  2. 2

    Add a class to the project by right-clicking on the Project Name in the Project Explorer and then selecting "Add Class Module."

  3. 3

    Set the name property of the class to "CFileReader" or a name using your coding standards.

  4. 4

    Add a reference to the Active Data Objects (ADO) library by selecting the "Project > References" menu and then selecting the "Microsoft ActiveX Data Objects 2.5 Library" or the "Microsoft ActiveX Data Objects 2.6 Library."

  5. 5

    Enter the following code in the class:

    Private Function ReadCSV(ByVal filename As String) As ADODB.Recordset

    Dim connection As ADODB.Connection

    Dim file Records As ADODB.Recordset

    Dim file Query As String

    Dim fieldNum As Integer

    'This drive supports Extended Properties='text;FMT=Delimited'"

    'There is also an additional argument, HDR with YES or NO values that indicates if the file has a header

    'connOpen "Provider=Microsoft.Jet" _

    ' & ".OLEDB.4.0;Data Source=" & App.Path _

    ' & ";Extended Properties='text;HDR=NO;" _

    ' & "FMT=Delimited'"

    Set connection = New ADODB.Connection
    connection.ConnectionString = _
        "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
        "DBQ=" & App.Path & ";", "", ""
    file Query = "Select * FROM " & filename
    Set file Records = connection.Execute(fileQuery)
    Set ReadCSV = file Records

    End Function

    This code connects to a text file using the Microsoft text driver and selects all of the rows into an ADO recordset.

  6. 6

    Call the function from the desired event or console application.

Tips and warnings

  • Encapsulating the code in a class allows it to be reused from many places.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.