CSV Standards to MDb

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
icthus17Asked:
Who is Participating?
 
leonstrykerCommented:
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
 
leonstrykerCommented:
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
 
icthus17Author Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
leonstrykerCommented:
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
 
icthus17Author Commented:
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
 
leonstrykerCommented:
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
 
icthus17Author Commented:
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
 
leonstrykerCommented:
>>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
 
icthus17Author Commented:
Thanks for the explanation of the place holders for the fields.

The last row is the one that's missing.
0
 
icthus17Author Commented:
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
 
leonstrykerCommented:
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
 
icthus17Author Commented:
I already tried that and it didn't work.
0
 
icthus17Author Commented:
Thanks a ton, Leon!  That worked perfectly.
0
 
leonstrykerCommented:
No problem, thanks for the grade,

Leon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.