XL: How to Obtain the SQL Statement for a PivotTable

Last reviewed: February 3, 1998
Article ID: Q131331
The information in this article applies to:
  • Microsoft Excel 98 Macintosh Edition
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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.

REFERENCES

For more information on the SourceData Property, click the Search button in Visual Basic Reference Help, and type:

   SourceData


Additional query words: XL5 XL7 XL97 XL98 5.00 5.00a 5.00c 7.00 7.00a
Keywords : kbcode kbprg kbualink97 xlquery PgmHowto
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.