SQL Connections To Tables Within Same Excel Spreadsheet

tjh392

New Member
Joined
Sep 9, 2013
Messages
7
Hi all,

I'm hoping that someone on here might be able to help.

I've created a workbook which contains tables of data on individual sheets (see example below)

9709278410_a5a674dffe_o.jpg


On Sheet1 I have an SQL statement which queries these tables using the connection "From Microsoft Query" and then I've chosen that same Excel workbook as the source and the tables within it.

9706064121_e94d81a5c9_o.jpg




Problem is if I move the workbook, all the hardcoded connections need updating to show the new file location which poses a problem when I will want to email this. Is there a way to make my connections dynamic so that they update and follow wherever I move the workbook?

9709348804_6d06f613a6_o.jpg


If I try refreshing the data without updating the connections to the new file location I get this error message:

9706117687_c930d67f3e_o.jpg


So to recap, I'm not actually connecting to an external source I'm only querying the tables within the same spreadsheet, perhaps there's a better way to do it? Any help would be appreciated!

Thanks

Tim
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I guess the first question would be, why are you using a query?

As far as updating the links goes, you'd need code to update the connection.
 
Upvote 0
Hi Rory, well the first sheet is basically a Division table which then separates the teams by division and underneath by the people who represent that area. This way the person who updates all the details can do it with minimal alterations to the front page. Also on other sheets it's shows projects occurring for each of those clubs so manual manipulation would be a lot more intensive than using these queries.

9706246945_20b09ebfb6_o.jpg
 
Upvote 0
OK - you're going to need code then. Something like this should do it:
Code:
Private Sub Workbook_Open()
   Dim oConn                       As WorkbookConnection
   Dim strOldFilePath              As String
   Dim n                           As Long
   Dim vParts

   For Each oConn In Me.Connections
      If oConn.Type = xlConnectionTypeODBC Then
         vParts = Split(oConn.ODBCConnection.Connection, ";")
         For n = LBound(vParts) To UBound(vParts)
            If Left$(UCase$(vParts(n)), 4) = "DBQ=" Then
               strOldFilePath = Mid$(vParts(n), 5)
               Exit For
            End If
         Next n

         If strOldFilePath <> Me.FullName Then
            With oConn.ODBCConnection
               .Connection = Replace$(.Connection, strOldFilePath, Me.FullName)
               .CommandText = Replace$(.CommandText, strOldFilePath, Me.FullName)
               .Refresh
            End With
         End If
      End If
   Next oConn

End Sub

Note: this must be placed in the ThisWorkbook module of the workbook project. It will then run automatically when the workbook is opened.
 
Upvote 0
I've just remembered that when I previously saved my workbook from .xlsx to .xlsm the SQL statements on the front page would run without showing an error but would not update the data within.

Is there something I can do to fix that first before implementing your code above?

Thanks
 
Upvote 0
The code should take care of that the next time you open the file.
 
Upvote 0
The code should take care of that the next time you open the file.

Thanks Rory, that worked a treat. I was able to run the SQL within the xlsm, initially I found that some of the SQL statements under the Command Text still had the old file location, whilst other's didn't. I realised that the ones that had updated had been aliased under the Name Manager so after adding all the tables using that it worked without any further alterations.

Thanks for your help.
 
Upvote 0
Thanks Rory, that worked a treat. I was able to run the SQL within the xlsm, initially I found that some of the SQL statements under the Command Text still had the old file location, whilst other's didn't. I realised that the ones that had updated had been aliased under the Name Manager so after adding all the tables using that it worked without any further alterations.

Thanks for your help.

Just got back round to altering this spreadsheet and I thought I best update the thread. The problem with some connections not updating in the SQL query was down to something a lot simpler than I thought. I didn't have to use aliases to make it work the SQL code just needed the hardcoded file location removing for it to work for example:

From:

Code:
SELECT 
   `Clubs Data$`.Team, 
   `Clubs Data$`.Division

FROM `E:\RFU Divisions 2013.xlsm`.`Clubs Data$` `Clubs Data$`

WHERE 
   (`Clubs Data$`.Division='Division C')

To:

Code:
SELECT 
   `Clubs Data$`.Team, 
   `Clubs Data$`.Division

FROM `Clubs Data$` `Clubs Data$`

WHERE
   (`Clubs Data$`.Division='Division C')

By removing the location at the start of the FROM the query is still able to run and is now update-able using the above macro.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top