Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default How to run a complex SQL statement against Excel spreadsheet range?

Hey All,

I'm trying to run a SQL statement against an Excel range. For example, say
we have a range with the following:

Name Year Earnings
Company1 1999 $25,000
Company2 1999 $30,000
Company3 1999 $35,000
Company1 2000 $27,000
Company2 2000 $32,000
Company3 2000 $37,000
Company1 2001 $29,000
Company2 2001 $34,000
Company3 2001 $39,000

And want to do something like:

SELECT Name, Year, Earnings FROM Selection WHERE Name='Company1'

The ADO code samples that I found do not have complex SQL statements and,
more importantly, do not work.

Thanks in Advance,

TC


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,118
Default How to run a complex SQL statement against Excel spreadsheet range

See if the QueryMaster file at Debra Dalgleish's website gets you pointed in
the right direction:
http://www.contextures.com/excelfiles.html#External

Use the Generic Excel List Query

Assuming you want to use the QueryMaster file as the data source, I inserted
a new worksheet (Sheet1) and copied your posted data to it.

Query Field Settings of Interest:
Data_Source: (enter the complete file path and file name )
SQL_Select: SELECT Name, Year, Earnings
SQL_From: "FROM `C:\ExcelQueries\QryMaster\QueryMaster`.`Sheet1$`"
SQL_Whe WHERE Name='Company1'

Note: The best approach is to create a named range for the data list and use
that name in the SQL_From field. But, if you refer to the sheet name followed
by a dollar sign (Sheet1$), the query will use the used range of the
referenced sheet and try to interpret it as a table.

Is that something you can work with?

Post back with any questions.

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey All,

I'm trying to run a SQL statement against an Excel range. For example, say
we have a range with the following:

Name Year Earnings
Company1 1999 $25,000
Company2 1999 $30,000
Company3 1999 $35,000
Company1 2000 $27,000
Company2 2000 $32,000
Company3 2000 $37,000
Company1 2001 $29,000
Company2 2001 $34,000
Company3 2001 $39,000

And want to do something like:

SELECT Name, Year, Earnings FROM Selection WHERE Name='Company1'

The ADO code samples that I found do not have complex SQL statements and,
more importantly, do not work.

Thanks in Advance,

TC



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default How to run a complex SQL statement against Excel spreadsheet range

Hey Ron,

Thanks for the assistance. Very nice sample file.

However, the Excel file referenced in the sample isn't in the .zip. Do you
have that sample data Excel file?

In answer to your question about referencing the sheet, unfortunately, for
my use case scenario, this wouldn't work. I'm being handed a file that has
a singular spreadsheet containing all of the tables but no named ranges.
I'll probably have to create the names myself. Is there no way to work with
the 'CurrentRegion' object and pass that as the source range?

Thanks Again,

Todd


"Ron Coderre" wrote in message
...
See if the QueryMaster file at Debra Dalgleish's website gets you pointed
in
the right direction:
http://www.contextures.com/excelfiles.html#External

Use the Generic Excel List Query

Assuming you want to use the QueryMaster file as the data source, I
inserted
a new worksheet (Sheet1) and copied your posted data to it.

Query Field Settings of Interest:
Data_Source: (enter the complete file path and file name )
SQL_Select: SELECT Name, Year, Earnings
SQL_From: "FROM `C:\ExcelQueries\QryMaster\QueryMaster`.`Sheet1$`"
SQL_Whe WHERE Name='Company1'

Note: The best approach is to create a named range for the data list and
use
that name in the SQL_From field. But, if you refer to the sheet name
followed
by a dollar sign (Sheet1$), the query will use the used range of the
referenced sheet and try to interpret it as a table.

Is that something you can work with?

Post back with any questions.

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey All,

I'm trying to run a SQL statement against an Excel range. For example,
say
we have a range with the following:

Name Year Earnings
Company1 1999 $25,000
Company2 1999 $30,000
Company3 1999 $35,000
Company1 2000 $27,000
Company2 2000 $32,000
Company3 2000 $37,000
Company1 2001 $29,000
Company2 2001 $34,000
Company3 2001 $39,000

And want to do something like:

SELECT Name, Year, Earnings FROM Selection WHERE Name='Company1'

The ADO code samples that I found do not have complex SQL statements and,
more importantly, do not work.

Thanks in Advance,

TC





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,118
Default How to run a complex SQL statement against Excel spreadsheet r

Todd

the Excel file referenced in the sample isn't in the .zip

Sorry for the confusion...I didn't actually change the file at Debra's site.
I described what I did to test a same-workbook query.

I'll probably have to create the names myself

Definitely

Is there no way to work with the 'CurrentRegion' object and pass that as the source range?

Not that I'm aware of. You'd need to create range names to be referenced by
the SQL.

I hope that helps

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey Ron,

Thanks for the assistance. Very nice sample file.

However, the Excel file referenced in the sample isn't in the .zip. Do you
have that sample data Excel file?

In answer to your question about referencing the sheet, unfortunately, for
my use case scenario, this wouldn't work. I'm being handed a file that has
a singular spreadsheet containing all of the tables but no named ranges.
I'll probably have to create the names myself. Is there no way to work with
the 'CurrentRegion' object and pass that as the source range?

Thanks Again,

Todd


"Ron Coderre" wrote in message
...
See if the QueryMaster file at Debra Dalgleish's website gets you pointed
in
the right direction:
http://www.contextures.com/excelfiles.html#External

Use the Generic Excel List Query

Assuming you want to use the QueryMaster file as the data source, I
inserted
a new worksheet (Sheet1) and copied your posted data to it.

Query Field Settings of Interest:
Data_Source: (enter the complete file path and file name )
SQL_Select: SELECT Name, Year, Earnings
SQL_From: "FROM `C:\ExcelQueries\QryMaster\QueryMaster`.`Sheet1$`"
SQL_Whe WHERE Name='Company1'

Note: The best approach is to create a named range for the data list and
use
that name in the SQL_From field. But, if you refer to the sheet name
followed
by a dollar sign (Sheet1$), the query will use the used range of the
referenced sheet and try to interpret it as a table.

Is that something you can work with?

Post back with any questions.

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey All,

I'm trying to run a SQL statement against an Excel range. For example,
say
we have a range with the following:

Name Year Earnings
Company1 1999 $25,000
Company2 1999 $30,000
Company3 1999 $35,000
Company1 2000 $27,000
Company2 2000 $32,000
Company3 2000 $37,000
Company1 2001 $29,000
Company2 2001 $34,000
Company3 2001 $39,000

And want to do something like:

SELECT Name, Year, Earnings FROM Selection WHERE Name='Company1'

The ADO code samples that I found do not have complex SQL statements and,
more importantly, do not work.

Thanks in Advance,

TC






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default How to run a complex SQL statement against Excel spreadsheet r

Hey Ron,

What about something like:


Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset


' Open a connection to the Excel workbook.
Set oConn = New ADODB.Connection
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Expenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
oConn.Open


' Create a command object and set its ActiveConnection
Set oCmd = New ADODB.Command
oCmd.ActiveConnection = oConn


' This SQL statement selects a cell range in a particular worksheet.
' oCmd.CommandText = "SELECT * from `Expenses$A2:C4`"


' This SQL statement selects a named cell range defined in the workbook.
oCmd.CommandText = "SELECT * from `Range1`"


' Open a recordset containing the worksheet data.
Set oRS = New ADODB.Recordset
oRS.CursorLocation = adUseServer
oRS.Open oCmd, , adOpenKeyset, adLockOptimistic


Debug.Print oRS.RecordCount


' Update last row
oRS.MoveLast
oRS(0).Value = -1
oRS.Update


' Add a new row
oRS.AddNew
oRS(0).Value = 7
oRS(1).Value = 8
oRS(2).Value = 9
oRS.Update


Debug.Print oRS.RecordCount


' Clean Up
Set oRS = Nothing
Set oCmd = Nothing
oConn.Close
Set oConn = Nothing


or like:

SELECT
Name, City
FROM [Sheet1$A1:D999]
IN 'C:\test.xls' 'EXCEL 8.0;'

Regards,

Todd


"Ron Coderre" wrote in message
...
Todd

the Excel file referenced in the sample isn't in the .zip

Sorry for the confusion...I didn't actually change the file at Debra's
site.
I described what I did to test a same-workbook query.

I'll probably have to create the names myself

Definitely

Is there no way to work with the 'CurrentRegion' object and pass that as
the source range?

Not that I'm aware of. You'd need to create range names to be referenced
by
the SQL.

I hope that helps

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey Ron,

Thanks for the assistance. Very nice sample file.

However, the Excel file referenced in the sample isn't in the .zip. Do
you
have that sample data Excel file?

In answer to your question about referencing the sheet, unfortunately,
for
my use case scenario, this wouldn't work. I'm being handed a file that
has
a singular spreadsheet containing all of the tables but no named ranges.
I'll probably have to create the names myself. Is there no way to work
with
the 'CurrentRegion' object and pass that as the source range?

Thanks Again,

Todd


"Ron Coderre" wrote in message
...
See if the QueryMaster file at Debra Dalgleish's website gets you
pointed
in
the right direction:
http://www.contextures.com/excelfiles.html#External

Use the Generic Excel List Query

Assuming you want to use the QueryMaster file as the data source, I
inserted
a new worksheet (Sheet1) and copied your posted data to it.

Query Field Settings of Interest:
Data_Source: (enter the complete file path and file name )
SQL_Select: SELECT Name, Year, Earnings
SQL_From: "FROM `C:\ExcelQueries\QryMaster\QueryMaster`.`Sheet1$`"
SQL_Whe WHERE Name='Company1'

Note: The best approach is to create a named range for the data list
and
use
that name in the SQL_From field. But, if you refer to the sheet name
followed
by a dollar sign (Sheet1$), the query will use the used range of the
referenced sheet and try to interpret it as a table.

Is that something you can work with?

Post back with any questions.

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey All,

I'm trying to run a SQL statement against an Excel range. For
example,
say
we have a range with the following:

Name Year Earnings
Company1 1999 $25,000
Company2 1999 $30,000
Company3 1999 $35,000
Company1 2000 $27,000
Company2 2000 $32,000
Company3 2000 $37,000
Company1 2001 $29,000
Company2 2001 $34,000
Company3 2001 $39,000

And want to do something like:

SELECT Name, Year, Earnings FROM Selection WHERE Name='Company1'

The ADO code samples that I found do not have complex SQL statements
and,
more importantly, do not work.

Thanks in Advance,

TC










  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,118
Default How to run a complex SQL statement against Excel spreadsheet r

Within the context of the QueryMaster.xls...there was very limited success.

For instance, referencing only a range (eg H21:J30) works if the range is on
the first sheet in the workbook. I couldn't get it to use a range reference
on a sheet that wasn't the first sheet. Named ranges worked as expected, no
matter where they were located. Referencing entire sheets (eg MySheet$) also
worked, but not with an associated range reference.

Of course, in specialized code, range references or ranges passed as
variables can be implemented, but the QueryMaster, being generic, is
designed to read text from the input cells and, consequenetly, is limited in
its ability to handle the other situations. I directed you to that file so
you could use it as a sandbox to play in.

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey Ron,

What about something like:


Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset


' Open a connection to the Excel workbook.
Set oConn = New ADODB.Connection
oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=Expenses.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes;"";"
oConn.Open


' Create a command object and set its ActiveConnection
Set oCmd = New ADODB.Command
oCmd.ActiveConnection = oConn


' This SQL statement selects a cell range in a particular worksheet.
' oCmd.CommandText = "SELECT * from `Expenses$A2:C4`"


' This SQL statement selects a named cell range defined in the workbook.
oCmd.CommandText = "SELECT * from `Range1`"


' Open a recordset containing the worksheet data.
Set oRS = New ADODB.Recordset
oRS.CursorLocation = adUseServer
oRS.Open oCmd, , adOpenKeyset, adLockOptimistic


Debug.Print oRS.RecordCount


' Update last row
oRS.MoveLast
oRS(0).Value = -1
oRS.Update


' Add a new row
oRS.AddNew
oRS(0).Value = 7
oRS(1).Value = 8
oRS(2).Value = 9
oRS.Update


Debug.Print oRS.RecordCount


' Clean Up
Set oRS = Nothing
Set oCmd = Nothing
oConn.Close
Set oConn = Nothing


or like:

SELECT
Name, City
FROM [Sheet1$A1:D999]
IN 'C:\test.xls' 'EXCEL 8.0;'

Regards,

Todd


"Ron Coderre" wrote in message
...
Todd

the Excel file referenced in the sample isn't in the .zip

Sorry for the confusion...I didn't actually change the file at Debra's
site.
I described what I did to test a same-workbook query.

I'll probably have to create the names myself

Definitely

Is there no way to work with the 'CurrentRegion' object and pass that as
the source range?

Not that I'm aware of. You'd need to create range names to be referenced
by
the SQL.

I hope that helps

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey Ron,

Thanks for the assistance. Very nice sample file.

However, the Excel file referenced in the sample isn't in the .zip. Do
you
have that sample data Excel file?

In answer to your question about referencing the sheet, unfortunately,
for
my use case scenario, this wouldn't work. I'm being handed a file that
has
a singular spreadsheet containing all of the tables but no named ranges.
I'll probably have to create the names myself. Is there no way to work
with
the 'CurrentRegion' object and pass that as the source range?

Thanks Again,

Todd


"Ron Coderre" wrote in message
...
See if the QueryMaster file at Debra Dalgleish's website gets you
pointed
in
the right direction:
http://www.contextures.com/excelfiles.html#External

Use the Generic Excel List Query

Assuming you want to use the QueryMaster file as the data source, I
inserted
a new worksheet (Sheet1) and copied your posted data to it.

Query Field Settings of Interest:
Data_Source: (enter the complete file path and file name )
SQL_Select: SELECT Name, Year, Earnings
SQL_From: "FROM `C:\ExcelQueries\QryMaster\QueryMaster`.`Sheet1$`"
SQL_Whe WHERE Name='Company1'

Note: The best approach is to create a named range for the data list
and
use
that name in the SQL_From field. But, if you refer to the sheet name
followed
by a dollar sign (Sheet1$), the query will use the used range of the
referenced sheet and try to interpret it as a table.

Is that something you can work with?

Post back with any questions.

***********
Regards,
Ron

XL2002, WinXP


"TCook" wrote:

Hey All,

I'm trying to run a SQL statement against an Excel range. For
example,
say
we have a range with the following:

Name Year Earnings
Company1 1999 $25,000
Company2 1999 $30,000
Company3 1999 $35,000
Company1 2000 $27,000
Company2 2000 $32,000
Company3 2000 $37,000
Company1 2001 $29,000
Company2 2001 $34,000
Company3 2001 $39,000

And want to do something like:

SELECT Name, Year, Earnings FROM Selection WHERE Name='Company1'

The ADO code samples that I found do not have complex SQL statements
and,
more importantly, do not work.

Thanks in Advance,

TC









Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
complex if statement kdel1201 Excel Worksheet Functions 1 November 8th 09 03:02 PM
Complex IF Statement Jonni Excel Discussion (Misc queries) 7 June 26th 09 04:09 PM
Complex IF THEN statement Greg S Excel Worksheet Functions 5 December 8th 08 11:40 PM
Complex If/And Statement jonssmaster Excel Worksheet Functions 8 December 1st 08 07:27 PM
Help with complex If statement GHawkins[_2_] Excel Worksheet Functions 2 September 6th 07 10:16 PM


All times are GMT +1. The time now is 01:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"