Solved

Copy an Excel range to a recordset using ADO

Posted on 2012-04-10
4
1,521 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 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 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 article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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 …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 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