Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to read Excel file with ADO?

Posted on 2002-05-14
8
Medium Priority
?
738 Views
Last Modified: 2013-12-25
I am trying to read an Excel file using ADO just as I have done with Access files for years.  The code I am using is listed below.  The problem is all the fields of the recordset are given the type adVarChar and numbers in the spreadsheet are not read into the recordset.  Only cells containing text are read in.  Recordset items corresponding to cells containing numbers are blank.  Can anyone suggest a fix?  


   Set oXLDBConn = New ADODB.Connection
   oXLDBConn.ConnectionString = "" & _
     "Provider=MSDASQL;" & _
     "DRIVER={Microsoft Excel Driver (*.xls)};" & _
     "DBQ=" & sWSheetFileSpec
   oXLDBConn.Open
 
   Set oXLRecset = New ADODB.Recordset
   sSQL = "SELECT * FROM " & sRangeName
   oXLRecset.Open sSQL, oXLDBConn, adOpenDynamic, adLockOptimistic


sWSheetFileSpec is the path to the spreadsheet file and sRangeName is the name given in the worksheet to the range of cells being read.


Remember, I want to do it with ADO if possible, not OLE Automation.  By-the-way, can ADO connect and read from text files as well?  Curently I manually create a recordset and use file I/O operations to populate it.
0
Comment
Question by:ASP_Ray
8 Comments
 
LVL 35

Accepted Solution

by:
David Todd earned 400 total points
ID: 7010304
Hi,

I'm wondering about using the excel object rather than using ADO.

That means that you can manipulate the cell contents using hte excel object model ...

I can give more info on this model if needed.

I don't knwo why you cant read the numbers from excel using ado.

Regards
  David
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7010510
Here is a connection string you can use for text files, essentially csv format.

"Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DBQ=C:\;DefaultDir=C:\;Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;Extensions=txt,csv,tab,asc;FIL=text;FILEDSN=C:\Program Files\Common Files\ODBC\Data Sources\Sheet.dsn;MaxBufferSize=2048;MaxScanRows=25;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"

Here is one I use for excel:

"Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DBQ=C:\Documents and Settings\ticotte\My Documents\Méribel.xls;DefaultDir=C:\Documents and Settings\ticotte\My Documents;Driver={Microsoft Excel Driver (*.xls)};DriverId=790;FIL=excel 8.0;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;ReadOnly=0;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
0
Technology Partners: 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!

 
LVL 1

Author Comment

by:ASP_Ray
ID: 7014270
Thanks for the help guys.  Please continue to provide your thoughts.

Now, I have continued to experiment with the problem and have come to some conclusions about the cause and the consequence of those causes.  Let me share them with you and I hope you will comment on it so we can learn.

This is my first attempt to access Excel files with ADO, my previous experience was entirely with Access databases, and I have now concluded that ADO IS INADEQUATE FOR ACCESSING EXCEL FILES.  Here is the reason.

Unlike Access, Excel does not prevent you from putting a value in a cell which is not compatible with the format of the cell.  You can put string "abc" in a cell formatted for currency, and this, I have found, is the source of the problem with ADO.  I found that whenever ADO encounters a value in a cell which is not compatible with the format of the cell, what ADO puts in the corresponding field of the recordset is weird.  On some occasions it puts nothing (I can deal with that), but most of the time it does things like put the value from the cell above (which in my case was compatible with its format) in that field.  This behavior makes it almost impossible to recognize that a problem occurred either programmatically or even by visually inspecting the result.  Because of this behavior then, I have concluded that, unless you can make sure that the source Excel file does not contain a value in a cell which is not compatible with the cell format, ADO is inadequate to use on it, since the errors in the result are deceptively difficult to notice.

What do you think?

I will release the points to the most helpful comment, or if someone can outline a practical scheme to force ADO to read an Excel file in a way where the problem fields are, at least, identifiable.

By the way, formatting all the cells as “General” does not solve the problem.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7014403
As you discovered, when using ADO to read Excel files, your files needs to be very clean. When I need to open and process XLS files, I mostly open the file into Excel and read values from my VB application. This way I can do every validation I need.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 7015057
Hi,

This echos my comment above as to using the Excel object to access the file ...

Regards
  David
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7060686
This question appears to be abandoned. A question regarding it will be left in the Community Support
area; if you have any comment about the question, please leave it here.

Unless there is objection or further activity, one of the moderators will be asked to accept the comment
of <dtodd>.

The link to the Community Support area is:
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

DO NOT ACCEPT THIS COMMENT AS AN ANSWER.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7065891
Thanks, emoreau  for your help here.

I have finalized this question, and will monitor it for comments.

Moondancer - EE Moderator
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

886 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