Improve company productivity with a Business Account.Sign Up


How to read Excel file with ADO?

Posted on 2002-05-14
Medium Priority
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
   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.
Question by:ASP_Ray
LVL 35

Accepted Solution

David Todd earned 400 total points
ID: 7010304

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.

LVL 43

Expert Comment

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;"
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Author Comment

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.
LVL 71

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.
LVL 35

Expert Comment

by:David Todd
ID: 7015057

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

LVL 71

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:


Expert Comment

ID: 7065891
Thanks, emoreau  for your help here.

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

Moondancer - EE Moderator

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

601 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