Read text files into ADO recordsets

Often, you may have cause to read a comma-delimited, or some other
form of delimitation, into an ADO recordset. However, if your text
file doesn't contain header information, you may encounter odd
behavior in which ADO uses data in the first record as the recordset's
field names.

This glitch results because, by default, ADO assumes that each text
file will contain headers on the first row. So how do you indicate
otherwise? One hint can be found in the connection string generated by
Visual Basic when it connects to a text file, as seen here:

connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
        & "Data Source=" & App.Path & ";" _
        & "Extended Properties='text;FMT=Delimited'"

As you can see, the connection string contains a section called
Extended Properties, which indicates a delimited text file. By adding
a third argument to this section, we can tell ADO that the file doesn't
contain headers. The argument named HDR takes YES or NO as its sole
value. With this extra argument in place, the completed connection
string looks like this:

connCSV.Open "Provider=Microsoft.Jet" _
        & ".OLEDB.4.0;Data Source=" & App.Path _
        & ";Extended Properties='text;HDR=NO;" _
        & "FMT=Delimited'"

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens