Solved

CSV Standards to MDb

Posted on 2004-08-09
14
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

737 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