Solved

CSV Standards to MDb

Posted on 2004-08-09
14
296 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:icthus17
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:icthus17
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I already tried that and it didn't work.
0
 
LVL 29

Accepted Solution

by:
leonstryker earned 500 total points
Comment Utility
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
Comment Utility
Thanks a ton, Leon!  That worked perfectly.
0
 
LVL 29

Expert Comment

by:leonstryker
Comment Utility
No problem, thanks for the grade,

Leon
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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

9 Experts available now in Live!

Get 1:1 Help Now