CommandType 属性 (Excel)QueryTable.CommandType property (Excel)

返回或设置下列**XlCmdType** 常量之一: xlCmdCubexlCmdDefaultxlCmdSqlxlCmdTableReturns or sets one of these XlCmdType constants: xlCmdCube, xlCmdDefault, xlCmdSql, or xlCmdTable. 返回或设置的常量描述**CommandText** 属性的值。The constant that is returned or set describes the value of the CommandText property. 默认值为 xlCmdSQLThe default value is xlCmdSQL. 可读/写 XlCmdTypeRead/write XlCmdType.

语法Syntax

表达式CommandTypeexpression.CommandType

_表达式_一个返回一个查询表**** 对象的表达式。expression An expression that returns a QueryTable object.

说明Remarks

仅当查询表或数据透视表缓存的**QueryType** 属性的值为xloledbquery 时时, 才能设置CommandType属性。You can set the CommandType property only if the value of the QueryType property for the query table or PivotTable cache is xlOLEDBQuery.

CommandType 属性的值为 xlCmdCube 时,如果没有与查询表相关联的数据透视表,则不能更改该值。If the value of the CommandType property is xlCmdCube, you cannot change this value if there is a PivotTable report associated with the query table.

如果使用用户界面导入数据, 则会将来自 web 查询或文本查询的数据作为一个查询表**** 对象导入, 而将所有其他外部数据作为**ListObject** 对象导入。If you import data by using the user interface, data from a web query or a text query is imported as a QueryTable object, while all other external data is imported as a ListObject object.

如果使用对象模型导入数据, 则必须将来自 web 查询或文本查询的数据作为查询表导入****, 而所有其他外部数据均可作为ListObject或查询表导入。 ****If you import data by using the object model, data from a web query or a text query must be imported as a QueryTable, while all other external data can be imported as either a ListObject or a QueryTable.

您可以使用ListObject的查询表属性来访问CommandType属性。 You can use the QueryTable property of the ListObject to access the CommandType property.

示例Example

本示例为第一张查询表的 ODBC 数据源设置命令字符串。This example sets the command string for the first query table's ODBC data source. 该命令字符串是一个 SQL 语句。The command string is an SQL statement.

Set qtQtrResults = _ 
 Workbooks(1).Worksheets(1).QueryTables(1) 
With qtQtrResults 
 .CommandType = xlCmdSQL 
 .CommandText = _ 
 "Select ProductID From Products Where ProductID < 10" 
 .Refresh 
End With

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.