Add Method (Parameters Collection)

Applies To

Parameters collection object.

Description

Creates a new query parameter. Returns a Parameter object that represents the new query parameter.

Syntax

expression.Add(Name, DataType)

expression Required. An expression that returns a Parameters object.

Name Required String. The name of the specified parameter. The parameter name should match the parameter clause in the SQL statement.

DataType Optional Variant. The data type of the parameter. Can be one of the following XlParameterDataType constants:

  • xlParamTypeBigInt
  • xlParamTypeBinary
  • xlParamTypeBit
  • xlParamTypeChar
  • xlParamTypeDate
  • xlParamTypeDecimal
  • xlParamTypeDouble
  • xlParamTypeFloat
  • xlParamTypeInteger
  • xlParamTypeLongVarBinary
  • xlParamTypeLongVarChar
  • xlParamTypeNumeric
  • xlParamTypeReal
  • xlParamTypeSmallInt
  • xlParamTypeTime
  • xlParamTypeTimeStamp
  • xlParamTypeTinyInt
  • xlParamTypeUnknown
  • xlParamTypeVarBinary
  • xlParamTypeVarChar

These values correspond to ODBC data types. They indicate the type of value the ODBC driver is expecting to receive. Microsoft Excel and the ODBC driver manager will coerce the parameter value given in Microsoft Excel into the correct data type for the driver.

Example

This example changes the SQL statement for query table one. The clause "(city=?)" indicates that the query is a parameter query, and the value of city is set to the constant "Oakland."

Set qt = Sheets("sheet1").QueryTables(1)
qt.Sql = "SELECT * FROM authors  WHERE (city=?)"
Set param1 = qt.Parameters.Add("City Parameter", xlParamTypeVarChar)
param1.SetParam xlConstant, "Oakland"
qt.Refresh