PC Review


Reply
Thread Tools Rate Thread

Accessing a sheet-level named range through ADO

 
 
Ahasverus
Guest
Posts: n/a
 
      18th Oct 2007

Hello,

I am trying to retrieve data from a workbook (xlsx) through ADO.

It works fine with ranges and workbook-level named ranges.

For instance, say that the workbook Hello.xlsx contains a range named "AnyData"
(defined at workbook level). This code works fine:

Const File = "C:\Temp\Hello.xlsx"
Dim Rs As New ADODB.Recordset
Dim Conn As New ADODB.Connection
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & File & _
";Extended Properties=""Excel 12.0;HDR=NO;IMEX=1"""
Rs.Open "SELECT * FROM AnyData", Conn

Now, suppose that "AnyData" is a worksheet-level name (attached to Sheet1, for
instance). How do you pass Sheet1!AnyData to the SELECT query?

I've tried :

Rs.Open "SELECT * FROM [Sheet1$AnyData]", Conn

.... but this query is rejected by the Jet engine.

If you do as if AnyData were a workbook-level name :

Rs.Open "SELECT * FROM AnyData", Conn

.... it works fine, but in this case, how can I access for instance another
"AnyData" named range which would be attached to another worksheet (say for
instance Sheet2!AnyData)?

Thanks !


 
Reply With Quote
 
 
 
 
Ahasverus
Guest
Posts: n/a
 
      18th Oct 2007

Solved !

The right syntax is : "SELECT * FROM [Sheet1$]AnyData"

Ahasverus a écrit :
>
> Hello,
>
> I am trying to retrieve data from a workbook (xlsx) through ADO.
>
> It works fine with ranges and workbook-level named ranges.
>
> For instance, say that the workbook Hello.xlsx contains a range named
> "AnyData" (defined at workbook level). This code works fine:
>
> Const File = "C:\Temp\Hello.xlsx"
> Dim Rs As New ADODB.Recordset
> Dim Conn As New ADODB.Connection
> Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & File & _
> ";Extended Properties=""Excel 12.0;HDR=NO;IMEX=1"""
> Rs.Open "SELECT * FROM AnyData", Conn
>
> Now, suppose that "AnyData" is a worksheet-level name (attached to
> Sheet1, for instance). How do you pass Sheet1!AnyData to the SELECT query?
>
> I've tried :
>
> Rs.Open "SELECT * FROM [Sheet1$AnyData]", Conn
>
> ... but this query is rejected by the Jet engine.
>
> If you do as if AnyData were a workbook-level name :
>
> Rs.Open "SELECT * FROM AnyData", Conn
>
> ... it works fine, but in this case, how can I access for instance
> another "AnyData" named range which would be attached to another
> worksheet (say for instance Sheet2!AnyData)?
>
> Thanks !
>
>

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      19th Oct 2007
Odd, it was how you had it in 2003 with Jet provider and Excel 8.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Ahasverus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Solved !
>
> The right syntax is : "SELECT * FROM [Sheet1$]AnyData"
>
> Ahasverus a écrit :
>>
>> Hello,
>>
>> I am trying to retrieve data from a workbook (xlsx) through ADO.
>>
>> It works fine with ranges and workbook-level named ranges.
>>
>> For instance, say that the workbook Hello.xlsx contains a range named
>> "AnyData" (defined at workbook level). This code works fine:
>>
>> Const File = "C:\Temp\Hello.xlsx"
>> Dim Rs As New ADODB.Recordset
>> Dim Conn As New ADODB.Connection
>> Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & File & _
>> ";Extended Properties=""Excel 12.0;HDR=NO;IMEX=1"""
>> Rs.Open "SELECT * FROM AnyData", Conn
>>
>> Now, suppose that "AnyData" is a worksheet-level name (attached to
>> Sheet1, for instance). How do you pass Sheet1!AnyData to the SELECT
>> query?
>>
>> I've tried :
>>
>> Rs.Open "SELECT * FROM [Sheet1$AnyData]", Conn
>>
>> ... but this query is rejected by the Jet engine.
>>
>> If you do as if AnyData were a workbook-level name :
>>
>> Rs.Open "SELECT * FROM AnyData", Conn
>>
>> ... it works fine, but in this case, how can I access for instance
>> another "AnyData" named range which would be attached to another
>> worksheet (say for instance Sheet2!AnyData)?
>>
>> Thanks !
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing a macro created named range John Microsoft Excel Programming 7 5th Feb 2009 04:12 PM
accessing named range greg Microsoft Excel Programming 13 12th Jul 2007 01:49 AM
Which Sheet Is Named Range On? Zone Microsoft Excel Programming 8 25th Jun 2007 05:19 PM
Named range in a different sheet Pereira Microsoft Excel Discussion 6 30th Mar 2007 10:07 AM
Named range in a sheet referred to from another sheet =?Utf-8?B?TGx1aXMgRXNjdWRl?= Microsoft Excel Programming 2 28th Jun 2006 12:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:45 PM.