import separated column from csv

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

import separated column from csv

Post by sal21 »

I have a csv delimited from ";" .
Into csv file are 8 column.
I need to import via ado and vb into a access table, with this condition:

Column2 csv into column7 access table
Column8 csv into column2 access table
Column4 csv into column9 access table
....
How to?

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

Re: import separated column from csv

Post by HansV »

Does the Access table already exist, or do you want to create a new table?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: import separated column from csv

Post by sal21 »

HansV wrote:Does the Access table already exist, or do you want to create a new table?
just exist and is named Table01

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

Re: import separated column from csv

Post by HansV »

Try code like this:

Dim cnn As New ADODB.Connection
Dim strSQL As String

' Your code to open a connection to the database
...
' Insert records from the text file
strSQL = "INSERT INTO Table01 (Field7, Field2, Field9) SELECT Field2, Field8, Field4 FROM [Text;Database=C:\TextFiles].[Test.csv]"
cnn.Execute strSQL

Of course, you must substitute the correct names of the fields, as well as the correct path and filename of the .csv file.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: import separated column from csv

Post by sal21 »

HansV wrote:Try code like this:

Dim cnn As New ADODB.Connection
Dim strSQL As String

' Your code to open a connection to the database
...
' Insert records from the text file
strSQL = "INSERT INTO Table01 (Field7, Field2, Field9) SELECT Field2, Field8, Field4 FROM [Text;Database=C:\TextFiles].[Test.csv]"
cnn.Execute strSQL

Of course, you must substitute the correct names of the fields, as well as the correct path and filename of the .csv file.
Hans before to test code... but the code consider the first line of csv as title of columns?
In other case i dont want to import the first line of csv into the table:-)

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

Re: import separated column from csv

Post by HansV »

The code assumes that the first line of the CSV file contains field names. You should use those field names in your code (in the SELECT Field2, Field8, Field4 FROM ... part)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: import separated column from csv

Post by sal21 »

HansV wrote:The code assumes that the first line of the CSV file contains field names. You should use those field names in your code (in the SELECT Field2, Field8, Field4 FROM ... part)
OK for fields name.
But have "invalid path name" error????????? why????


strSQL = "INSERT INTO GAF (NDG, SPORTELLO, RAPPORTO) SELECT NDG, SPORTELLO, RAPPORTO FROM [Text;Database=C:\dir1\csv_prodotti_TEST].[Test.csv]"

Debug.Print strSQL
GAF_CONN.Execute strSQL
Last edited by sal21 on 04 May 2010, 10:13, edited 1 time in total.

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

Re: import separated column from csv

Post by HansV »

The first part between square brackets should be the path, and the second part the filename, so you should use

[Text;Database=C:\PETTI].[csv_prodotti_TEST.csv]
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: import separated column from csv

Post by sal21 »

HansV wrote:Try code like this:

Dim cnn As New ADODB.Connection
Dim strSQL As String

' Your code to open a connection to the database
...
' Insert records from the text file
strSQL = "INSERT INTO Table01 (Field7, Field2, Field9) SELECT Field2, Field8, Field4 FROM [Text;Database=C:\TextFiles].[Test.csv]"
cnn.Execute strSQL

Of course, you must substitute the correct names of the fields, as well as the correct path and filename of the .csv file.
Hans... based this command i need to insert in Table01 a new dta from a variable and not from csv file field... How to?
Example:

strSQL = "INSERT INTO Table01 (Field7, Field2, Field9,Field10) SELECT Field2, Field8, Field4, '" & myvar &"' FROM [Text;Database=C:\TextFiles].[Test.csv]"

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

Re: import separated column from csv

Post by HansV »

What kind of field do you want to insert the data into? Text, number, date, ...?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: import separated column from csv

Post by sal21 »

HansV wrote:What kind of field do you want to insert the data into? Text, number, date, ...?
is a date ...

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

Re: import separated column from csv

Post by HansV »

Try

strSQL = "INSERT INTO Table01 (Field7, Field2, Field9,Field10) SELECT Field2, Field8, Field4, #" & Format(myvar, "mm/dd/yyyy") & "# FROM [Text;Database=C:\TextFiles].[Test.csv]"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: import separated column from csv

Post by sal21 »

HansV wrote:Try

strSQL = "INSERT INTO Table01 (Field7, Field2, Field9,Field10) SELECT Field2, Field8, Field4, #" & Format(myvar, "mm/dd/yyyy") & "# FROM [Text;Database=C:\TextFiles].[Test.csv]"
oK!
i cannot test now because have a prob on delimited separato....
In effect i dont have ; in csv but ";"

with this type of separator work
qqqqq;eseeee;......

i really have this structure of csv file
"qqqqq";"eseeee";"......