ADO import csv line by line

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

ADO import csv line by line

Post by agibsonsw »

Hello. Access 2003.
Does someone have sample code to import a csv file using ADO, and to
either append (or create a table and then append) row by row?
Basically, I want to append rows one by one but to perform some validation
as this is done.
Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ADO import csv line by line

Post by HansV »

Try something like this:

Code: Select all

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
cnn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=H:\TextFiles"
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM [Test.csv]", cnn, adOpenForwardOnly, adLockOptimistic, adCmdText
Do While Not rst.EOF
  ' Do something with record here
  ...
  rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set cnn = Nothing
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: ADO import csv line by line

Post by agibsonsw »

Perfect, thanks. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.