Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

CSV Standards to MDb

Posted on 2004-08-09
14
Medium Priority
?
316 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

578 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