Microsoft KB Archive/131331

= How to Obtain the SQL Statement for a PivotTable =

Article ID: 131331

Article Last Modified on 8/15/2005

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q131331



This article also applies to:
 * Microsoft Excel for Windows 5.0c|5.0c
 * Microsoft Excel for Windows 95 7.0a|7.0a
 * Microsoft Excel for Windows NT 5.0|5.0
 * Microsoft Excel for the Macintosh 5.0|5.0
 * Microsoft Excel for the Macintosh 5.0a|5.0a





SUMMARY
When you are working with a Microsoft Excel PivotTable, you may want to determine its data source. To do this, use the SourceData property in Microsoft Visual Basic for Applications code.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. A Microsoft Excel PivotTable can be based on a result set obtained via Microsoft Query from an external data source. When a PivotTable is created in this way, a Structured Query Language (SQL) SELECT statement is created. The SELECT statement describes which fields to use and from which table to select them. It also specifies any criteria that are applied to the result set.

The SourceData property can be used to return the data source for a PivotTable object. If the data source is an external data source, then the return value for the SourceData property is an array that consists of an SQL connection string with the remaining elements as the query string broken into 200-character segments. For example, if you create a PivotTable that uses data from the NWind data source, and you want to see records from the Orders table with the following criteria


 * The records are from the Employee Id and Order Amounts fields
 * The Order Date of the records is greater than or equal to June 1, 1989

the SELECT statement for this query would resemble the following:

SELECT orders.EMPLOY_ID, orders.ORDER_AMT

FROM c:\windows\msapps\msquery\orders.dbf orders

WHERE (orders.ORDER_DATE>={d '1989-06-01'})

To return the SQL connection string and SELECT statement for this sample PivotTable, you could create a macro similar to the following: Sub GetSourceData Dim SQLString As Variant Dim RowCount As Integer Dim SQLRange As Range

Set SQLRange = Range("Sheet1!A1")

' Assign the SourceData array to the SQLString variable. SQLString = SQLRange.PivotTable.SourceData

' Loop through each element of the SQLString array and copy these ' elements to Sheet1, starting in cell A1 and going down.

RowCount = 0 For Each xElement In SQLString ' The first element is the Connection String. ' Each additional element is the SELECT Statement ' broken in to 200-character text strings. Range("A1").Offset(RowCount, 0).Value = xElement RowCount = RowCount + 1 Next End Sub Note that if the first element, which is the Connection String, is greater than 255 characters, it will be truncated. However, each additional element makes up the SELECT statement and these elements are broken into strings of 200 characters each.

