Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Copy an Excel range to a recordset using ADO

Posted on 2012-04-10
4
Medium Priority
?
1,701 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
[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
  • 3
4 Comments
 
LVL 42

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 42

Accepted Solution

by:
dlmille earned 2000 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 42

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

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!

Question has a verified solution.

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

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

610 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