Solved

Copy an Excel range to a recordset using ADO

Posted on 2012-04-10
4
1,220 Views
Last Modified: 2012-04-10
I am trying to retrieve the contents of a range in an excel worksheet into a recordset using ADO in C#.  I am using Excel 2007. This is what I have:

SQLquerySource = "SELECT * FROM [Sheet1$SourceRange]"  //"SourceRange" is a named range

ADODB.Connection cn =new ADODB.Connection();
cn.Open(ExcelSourceConnection, null, null, 0);  //"ExcelSourceConnection" identifies the source workbook
ADODB.Recordset rs = new ADODB.Recordset();
rs.Open(SQLquerySource, cn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, -1); //at this statement I get a "COM Exception was unhandled" and its description says "The Microsoft Office Access database engine could not find the object 'Sheet1$SourceRange'.  Make sure the object exists and that you spell its name and the path name correctly."

Of course, when I open the source workbook and check, I can see both the source sheet and the named range are valid, and I have spelled them correctly, so there must be more to it than that.

But what?
0
Comment
Question by:bassman592
  • 3
4 Comments
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
You don't need the Sheet1$ prefix.  The below assumes SourceRange is scoped at the Workbook (rather than worksheet) level.

SQLquerySource = "SELECT * FROM [SourceRange]"  //"SourceRange" is a named range


Dave
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
Comment Utility
If SourceRange is locally scoped to Sheet1, you have to do a bit more work.  First, get the range address of SourceRange, then use your Sheet1$ prefix.

VBA example:

    strSource = "Sheet1$" & Replace([Sheet1!SourceRange].Address, "$", "")
    strSQL = "SELECT * from [" & strSource & "]"


Cheers,

Dave
0
 

Author Closing Comment

by:bassman592
Comment Utility
Sorry, I should have stated that the named range is global scope, but the additional information is useful. I actually was thinking of making some local, but didn't think I would be able to accurately reference them.

Thanks!
0
 
LVL 41

Expert Comment

by:dlmille
Comment Utility
You're welcome.  I was stumped on the local variable as well, then had to do some testing to get it to local scope.  You almost have to know its local to go local, otherwise you have to do some names backflips to determine if the parent is the workbook or a sheet, etc...

Dave
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

743 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

16 Experts available now in Live!

Get 1:1 Help Now