Solved

How to read Excel file with ADO?

Posted on 2002-05-14
8
724 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 100 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
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 7010619
0
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 69

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 69

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now