Solved

CSV Standards to MDb

Posted on 2004-08-09
14
300 Views
Last Modified: 2013-12-25
I am trying to create a routine to import CSV files into an Access Database, but I want the process to account for all of the CSV standards listed at http://www.edoceo.com/utilis/csv-file-format.php

The problem is that the CSV files I'll be opening will have 42 fields.  So, do I have to read all 42 individually, or is there an easier way?  I'd like all of the fields accessible by variable so that I can then put them in the right place in the database (assuming this is the best way?).

Also, I'd like the process to simply skip a line if there is an error.

Thanks for any help!

Branton
0
Comment
Question by:icthus17
  • 7
  • 7
14 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 11754944
The best way to do this, IMO, would be to connect to your CSV files with ADO and to copy the data over using SQL Insert statements.  Here is a connection string you can use to connect to the file

http://www.able-consulting.com/MDAC/ADO/Connection/OLEDB_Providers.htm#OLEDBProviderForMicrosoftJetText

Leon
0
 

Author Comment

by:icthus17
ID: 11755349
leonstryker,

I had already tried the exacmple from the page, but I can't get it to work.  I get the error: 'C:\path\textfile.txt' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

I know the path is correct, because before this process, it is selected through a file open dialogue box, and I can read from the file using other methods.

I was hoping that I might be able to do something similar to what I've done in PHP/MySQL:

load data local infile '" . $csv_file_add . "' replace into table instructors
                        fields terminated by ','
                        optionally enclosed by '\"'
                        escaped by '\\\\'
                        lines terminated by '\\r\\n'
                        ignore 1 lines";

But even if I could get the CSV file loaded into a ADODB object I can go from there.

Thanks,

Branton
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11755626
Try the ODBC driver it will be easier:

oConn.Open _
    "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    "Dbq=c:\somepath\;" & _
    "Extensions=asc,csv,tab,txt"
Then specify the filename in the SQL statement:

oRs.Open "Select * From customer.csv", _
         oConn, adOpenStatic, adLockReadOnly, adCmdText

Once you return the recordset you can concatenate a SQL Insert string:

strSQL = "INSERT INTO myTable VALUES('" & oRs!Field1 & "'," ....

You then execute this against you connection to the database in a Loop:

cnConnection.Execute strSQL
oRs.MoveNext


Leon
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:icthus17
ID: 11762665
Leon,

That is exactly what I want, but I still get errors:

[Microsoft][ODBC Text Driver]'(unknown)' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

This appears to be a similar problem to before.  This is the code where I specify the file (FileName2 is selected from the file open dialogue):

            oConn.Open _
                "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
                "Dbq=" & FileName2 & ";" & _
                "Extensions=asc,csv,tab,txt"
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11762896
We need to take a look at the FileName2  variable.  Assign the string to a variable strConnection and look at it before passing it to the Open method:

strSQL = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
                "Dbq=" & FileName2 & ";" & _
                "Extensions=asc,csv,tab,txt"

Debug.Print strSQL

oConn.Open strSQL

Leon
0
 

Author Comment

by:icthus17
ID: 11763181
Okay, I've solved one problem and come up with another.  I didn't realize that Dbq required a directory path, not the full path to the file (FileName2).  That's fixed and working.  I also couldn't get fields by oRs!Field1 ..., but got it to work with oRs.Fields(0) ...

Now, the new problem.  The process puts the data into the database, but it misses the last line.  Directly after my SQL SELECT statement, the recordset only shows 3 records when there should be 4.  Here's the code:

            oRs.Open _
                "SELECT * FROM " & FileName2, _
                oConn, adOpenStatic, adLockReadOnly, adCmdText

Here, oRs.RecordCount = 3 (should be 4)

Any ideas?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11763269
>>oRs!Field1 ..., but got it to work with oRs.Fields(0)

Field1 is just a place holder for the name of the field, so if your first field header is aField, it would have been oRs!aField.  Which brings us to the question:  Is it the first row which is missing?

Leon
0
 

Author Comment

by:icthus17
ID: 11763336
Thanks for the explanation of the place holders for the fields.

The last row is the one that's missing.
0
 

Author Comment

by:icthus17
ID: 11763481
Very sorry.  It's actually the first row that's missing.  Is it pulling the first row as a header row?  If so, how can I prevent that?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11763660
Try adding HDR=No; to your connection string:

strSQL = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
                "Dbq=" & FileName2 & ";" & _
                "Extensions=asc,csv,tab,txt;HDR=No"

Leon
0
 

Author Comment

by:icthus17
ID: 11763727
I already tried that and it didn't work.
0
 
LVL 29

Accepted Solution

by:
leonstryker earned 500 total points
ID: 11763759
It looks like you will need to use the Jet driver if you do not have a header row;

http://www.freevbcode.com/ShowCode.asp?ID=2180&NoBox=True

Leon
0
 

Author Comment

by:icthus17
ID: 11764392
Thanks a ton, Leon!  That worked perfectly.
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 11764481
No problem, thanks for the grade,

Leon
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now