Solved

Copy an Excel range to a recordset using ADO

Posted on 2012-04-10
4
1,340 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

813 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