Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default using worksheet in same workbook for query

Due to an "unusual" situation, I have had to setup a workbook as an RDBMS.

worksheets are treated as tables.

Using the MS-Query functionality in excel, everything works fine except when
I try to send the file to other people, the workbook is unable to reference
itself as the source of the MS-Query and I think looks for the absolute path
of the file on my machine.

Is there a way to point to the same worksheet "relatively"?

Hope I'm making sense :) thanks in advance
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default using worksheet in same workbook for query

In the VBA editor, type in the immediate pane
? Activesheet.Querytables(1).CommandText
(this assumes the active sheet is the one with the query results and that
there is only the one query table on the sheet)
The CommandText gives the connection string that specifies, among other
things, the file path. You will need to write code to duplicate the string
but replace the path. The path for the workbook in use is
ThisWorkbook.FullName, so you can use that to change the path showing in the
CommandText. To ensure it is up to date, you should really check it just
before the query is run (since the user could have resaved the file under a
new name). If you use the option to refresh when a parameter linked to a
cell changes, you may want to run the code to do this in the Worksheet_Change
event procedure; or if you use another macro to refresh the query you can put
it in there. Example below:

Let's say the string you get for the CommandText is:
SELECT MyTable.`Header 1`, MyTable.'Header 2' FROM `C:\Excel\MyFile`.MyTable
MyTable

Before running the query, run this line of code:
Worksheets("SheetName").QueryTables(1).CommandText = _
"SELECT MyTable.`Header 1`, MyTable.'Header 2' FROM '" _
& ThisWorkbook.FullName & "'`.MyTable MyTable"

That should update the query to use the current file instead of the
originally saved one.
Hope this works for you.

--
- K Dales


"Jaypee" wrote:

Due to an "unusual" situation, I have had to setup a workbook as an RDBMS.

worksheets are treated as tables.

Using the MS-Query functionality in excel, everything works fine except when
I try to send the file to other people, the workbook is unable to reference
itself as the source of the MS-Query and I think looks for the absolute path
of the file on my machine.

Is there a way to point to the same worksheet "relatively"?

Hope I'm making sense :) thanks in advance

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default using worksheet in same workbook for query

Thanks! That did it!

"K Dales" wrote:

In the VBA editor, type in the immediate pane
? Activesheet.Querytables(1).CommandText
(this assumes the active sheet is the one with the query results and that
there is only the one query table on the sheet)
The CommandText gives the connection string that specifies, among other
things, the file path. You will need to write code to duplicate the string
but replace the path. The path for the workbook in use is
ThisWorkbook.FullName, so you can use that to change the path showing in the
CommandText. To ensure it is up to date, you should really check it just
before the query is run (since the user could have resaved the file under a
new name). If you use the option to refresh when a parameter linked to a
cell changes, you may want to run the code to do this in the Worksheet_Change
event procedure; or if you use another macro to refresh the query you can put
it in there. Example below:

Let's say the string you get for the CommandText is:
SELECT MyTable.`Header 1`, MyTable.'Header 2' FROM `C:\Excel\MyFile`.MyTable
MyTable

Before running the query, run this line of code:
Worksheets("SheetName").QueryTables(1).CommandText = _
"SELECT MyTable.`Header 1`, MyTable.'Header 2' FROM '" _
& ThisWorkbook.FullName & "'`.MyTable MyTable"

That should update the query to use the current file instead of the
originally saved one.
Hope this works for you.

--
- K Dales


"Jaypee" wrote:

Due to an "unusual" situation, I have had to setup a workbook as an RDBMS.

worksheets are treated as tables.

Using the MS-Query functionality in excel, everything works fine except when
I try to send the file to other people, the workbook is unable to reference
itself as the source of the MS-Query and I think looks for the absolute path
of the file on my machine.

Is there a way to point to the same worksheet "relatively"?

Hope I'm making sense :) thanks in advance

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default using worksheet in same workbook for query

ok, when I tried it on my actual project... it was not working.

for some reason, my worksheets are treated as "system tables" and when I
copy it to a different machine, it cannot read it.

"Jaypee" wrote:

Thanks! That did it!

"K Dales" wrote:

In the VBA editor, type in the immediate pane
? Activesheet.Querytables(1).CommandText
(this assumes the active sheet is the one with the query results and that
there is only the one query table on the sheet)
The CommandText gives the connection string that specifies, among other
things, the file path. You will need to write code to duplicate the string
but replace the path. The path for the workbook in use is
ThisWorkbook.FullName, so you can use that to change the path showing in the
CommandText. To ensure it is up to date, you should really check it just
before the query is run (since the user could have resaved the file under a
new name). If you use the option to refresh when a parameter linked to a
cell changes, you may want to run the code to do this in the Worksheet_Change
event procedure; or if you use another macro to refresh the query you can put
it in there. Example below:

Let's say the string you get for the CommandText is:
SELECT MyTable.`Header 1`, MyTable.'Header 2' FROM `C:\Excel\MyFile`.MyTable
MyTable

Before running the query, run this line of code:
Worksheets("SheetName").QueryTables(1).CommandText = _
"SELECT MyTable.`Header 1`, MyTable.'Header 2' FROM '" _
& ThisWorkbook.FullName & "'`.MyTable MyTable"

That should update the query to use the current file instead of the
originally saved one.
Hope this works for you.

--
- K Dales


"Jaypee" wrote:

Due to an "unusual" situation, I have had to setup a workbook as an RDBMS.

worksheets are treated as tables.

Using the MS-Query functionality in excel, everything works fine except when
I try to send the file to other people, the workbook is unable to reference
itself as the source of the MS-Query and I think looks for the absolute path
of the file on my machine.

Is there a way to point to the same worksheet "relatively"?

Hope I'm making sense :) thanks in advance

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
another macro query - deleting a worksheet within a query DavidHawes Excel Discussion (Misc queries) 2 February 26th 07 10:05 AM
Query data from same workbook JT Charts and Charting in Excel 0 October 6th 06 12:23 AM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 1 November 29th 05 01:44 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 0 November 28th 05 06:37 PM
Query returning more data than will fit on worksheet with VBA DB query... ChrisSmith Excel Programming 0 June 8th 04 12:07 PM


All times are GMT +1. The time now is 06:30 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"