Solved

How to read Excel file with ADO?

Posted on 2002-05-14
8
732 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
[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
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 70

Expert Comment

by:Éric Moreau
ID: 7010619
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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.
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…

751 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