How to Obtain the SQL Statement for a PivotTable This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
This article was previously published under Q131331 This article also applies to:
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 INFORMATIONMicrosoft 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
SELECT orders.EMPLOY_ID, orders.ORDER_AMT
To return the SQL connection string and SELECT statement for this sample
PivotTable, you could create a macro similar to the following:
FROM c:\windows\msapps\msquery\orders.dbf orders WHERE (orders.ORDER_DATE>={d '1989-06-01'})
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
APPLIES TO
| Article Translations
|


Back to the top
