1

I am using the below code to import a CSV file to my Access DB. I just have a couple of questions.

    Con.Open()
    Dim strSqlCommand = "SELECT F1 AS id, F2 AS firstname " &
                        "INTO MyNewTable " &
                        "FROM [Text;FMT=Delimited;HDR=No;CharacterSet=850;DATABASE=" & GlobalVariables.strDefaultDownloadPath & "].Airports.csv;"
    Dim sqlCommand = New System.Data.OleDb.OleDbCommand(strSqlCommand, Con)
    sqlCommand.ExecuteNonQuery()
    Con.Close()

How can I change the Character Set to UTF-8? If I enter utf8 instead of 850 I get an error.

Also, the first line of my CSV file contains the column names. Can I amend the above code to take that in to account?

Regards,

Andrew

2
  • What error?... also you say you are entering utf8 try UTF-8. For column names change the header parameter: HDR=YES Commented Feb 20, 2015 at 17:38
  • I get an invalid argument error whenever I change the character set to any variation of UTF-8. It so far accepts 850 and ANSI but these do not show any accented characters properly after the import. Commented Feb 20, 2015 at 19:11

1 Answer 1

1

You could run into trouble trying to import and select all at once, for one thing you may not want to leave converting data types up to Access. For that, you will need 2 connections and SQL string to select from one another to insert into the other.

The connection string will need to look something like this:

 "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp\Tmp;Extended Properties='TEXT;HDR=Yes;FMT=Delimited;CharacterSet=ANSI'"

Note that just the path is listed and the Extended Properties are enclosed in ticks. If the first line has headers/field names then HDR=Yes will skip them in the result set. One of the benefits of having field names as the first line is that OleDB will use them as column names (no need for F1 As foo, F2 As bar; in fact that will fail because they have been renamed from F1, F2...).

The SQL to read from the CSV:

 "SELECT * FROM filename.csv"

There are several ways to process it. You could use a reader to read a row at a time to INSERT them into the Access database. This is probably simpler: get all the data from the CSV into a DataTable and use it to INSERT into Access:

Private myDT As DataTable    ' form level variable

...
Dim csvStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Temp\Tmp;Extended Properties='TEXT;HDR=Yes;FMT=Delimited;CharacterSet=ANSI'"
Dim csvSQL = "SELECT * FROM Capitals.csv"    ' use YOUR file name

Using csvCn = New OleDbConnection(csvStr),
         cmd As New OleDbCommand(csvSQL, csvCn)

    Using da As New OleDbDataAdapter(cmd)
        myDT = New DataTable
        da.Fill(myDT)
    End Using
End Using

For Each r As DataRow In myDT.Rows
    'ToDo: INSERT INTO Access
Next

The Connection, Command and DataAdapter are all resources, so they are in USING blocks to dispose of them when we are done with them. myDT will have a collection of Rows, each with a collection of Items representing the fields from the CSV. Just loop thru the rows adding the desired items to the Access DB.

You will very likely have to do same data type conversion from String to Integer or DateTime etc.

As for the question about UTF8 - you can use the Codepage identifier. If you leave it off the connection string it will use whatever is in the Registry which may also work. For UTF8 use CharacterSet=65001.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, I changed HDR to yes and my SELECT statement to SELECT * INTO MyNewTable and its copying over great. It has over 46000 records and it does it nice and fast. However, I still have an issue with the character set, regardless of what I use, the accented letter do not appear correctly. If I did this task manually in Access I would choose UTF-8 and after the import it all appeared fine.
It is working great now, thank you for that! Was searching for ages for a page that shows all of the codes, don't know how I didn't find it. Once again, thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.