Solved

Copy an Excel range to a recordset using ADO

Posted on 2012-04-10
4
1,272 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
ID: 37830115
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
ID: 37830152
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
ID: 37830465
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
ID: 37830471
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

929 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

12 Experts available now in Live!

Get 1:1 Help Now