Help with TRANSFORM in my SQL

mikemny

New Member
Joined
Sep 24, 2008
Messages
16
Hello,

I am new to using SQL (and still learning!), and this is my first encounter with the TRANSFORM functionality. I am querying an ODBC database, returning the results in Excel. I was able to get the results I need in Access, but need the query in excel as the inputs will ultimately be dynamic based on other information in the excel file.

I based the below code on the SQL from Access, but am having difficulties. The query returns results (just not pivoted) when I remove the TRANSFORM and PIVOT lines. When I add them, I get a SQL Syntax Error with the line .Refresh BackgroundQuery:=False highlighted.

Rich (BB code):
Sub Macro()
Dim strSQL As String
 
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
      "ODBC;DSN=firm_prod;NA=firmprod1,6100;DB=db_firmprod;UID=X1234;", _
      Destination:=Range("$C$22")).QueryTable
 
      strSQL = "TRANSFORM Sum(report_name.col8) AS (IssSpc)" _
      & " SELECT report_name.row" _
      & " FROM db_firmprod.dbo.report_name report_name" _
      & " WHERE " _
          & "(report_name.choiceID='ABC')" _
      & " GROUP BY  report_name.row" _
      & " ORDER BY  report_name.choiceID, report_excess_return.row" _
      & " PIVOT report_name.choiceID" _
 
      .CommandText = strSQL
 
      .RowNumbers = False
      .FillAdjacentFormulas = False
      .PreserveFormatting = True
      .RefreshOnFileOpen = False
      .BackgroundQuery = True
      .RefreshStyle = xlInsertDeleteCells
      .SavePassword = False
      .SaveData = True
      .AdjustColumnWidth = True
      .RefreshPeriod = 0
      .PreserveColumnInfo = True
      .ListObject.DisplayName = "Table_Query_from_firm_prod5"
      .Refresh BackgroundQuery:=False
  End With
End Sub

Any advice would be greatly appreciated!

Thanks!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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