How to read Excel file with ADO?

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.
Who is Participating?
David ToddSenior DBACommented:

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.

TimCotteeHead of Software ServicesCommented:
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;"
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ASP_RayAuthor Commented:
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.
Éric MoreauSenior .Net ConsultantCommented:
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.
David ToddSenior DBACommented:

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

Éric MoreauSenior .Net ConsultantCommented:
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:

Thanks, emoreau  for your help here.

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

Moondancer - EE Moderator
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.

All Courses

From novice to tech pro — start learning today.