PARAMETERS 声明 (Microsoft Access SQL)
适用于:Access 2013、Office 2013
声明在参数查询中的每个参数的名称和数据类型。
语法
PARAMETERS name datatype [, name datatype [, ...]]
PARAMETERS 声明包含以下部分:
Part |
说明 |
---|---|
name |
参数的名称。 分配给 Parameter 对象的 Name 属性,用于在 Parameters 集合中标识此参数。 可以将 名称 用作在应用程序运行查询时显示在对话框中的字符串。 使用方括号 ([ ]) 将包含空格或标点符号的文本括起来。 例如,[低价] 和 [从哪个月份开始报表?] 是有效的 名称 参数。 |
datatype |
主要 Microsoft Access SQL 数据类型或其同义词之一。 |
说明
对于定期运行的查询,可以通过 PARAMETERS 声明来创建一个参数查询。 参数查询能够自动处理查询条件更改。 若使用参数查询,在每次查询运行时代码都需要提供参数。
PARAMETERS 声明是可选的,但如果包含它,应将它置于任何其他语句(包括 SELECT 语句)之前。
如果声明包含了多个参数,请用逗号分隔它们。 以下的示例里包含了两个参数:
PARAMETERS [Low price] Currency, [Beginning date] DateTime;
在 WHERE 或 HAVING 子句中可以使用 name 参数,不能使用 datatype 参数。 以下的示例中要求提供两个参数,然后将该条件应用于 Orders 表的记录中:
PARAMETERS [Low price] Currency,
[Beginning date] DateTime;
SELECT OrderID, OrderAmount
FROM Orders
WHERE OrderAmount > [Low price]
AND OrderDate >= [Beginning date];
示例
本示例要求用户提供职务,然后使用该职务作为查询条件。
它调用 EnumFields 过程,您可以在 SELECT 语句示例中找到该过程。
Sub ParametersX()
Dim dbs As Database, qdf As QueryDef
Dim rst As Recordset
Dim strSql As String, strParm As String
Dim strMessage As String
Dim intCommand As Integer
' Modify this line to include the path to Northwind
' on your computer.
Set dbs = OpenDatabase("NorthWind.mdb")
' Define the parameters clause.
strParm = "PARAMETERS [Employee Title] CHAR; "
' Define an SQL statement with the parameters
' clause.
strSql = strParm & "SELECT LastName, FirstName, " _
& "EmployeeID " _
& "FROM Employees " _
& "WHERE Title =[Employee Title];"
' Create a QueryDef object based on the
' SQL statement.
Set qdf = dbs.CreateQueryDef _
("Find Employees", strSql)
Do While True
strMessage = "Find Employees by Job " _
& "title:" & Chr(13) _
& " Choose Job Title:" & Chr(13) _
& " 1 - Sales Manager" & Chr(13) _
& " 2 - Sales Representative" & Chr(13) _
& " 3 - Inside Sales Coordinator"
intCommand = Val(InputBox(strMessage))
Select Case intCommand
Case 1
qdf("Employee Title") = _
"Sales Manager"
Case 2
qdf("Employee Title") = _
"Sales Representative"
Case 3
qdf("Employee Title") = _
"Inside Sales Coordinator"
Case Else
Exit Do
End Select
' Create a temporary snapshot-type Recordset.
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
' Populate the Recordset.
rst.MoveLast
' Call EnumFields to print the contents of the
' Recordset. Pass the Recordset object and desired
' field width.
EnumFields rst, 12
Loop
' Delete the QueryDef because this is a
' demonstration.
dbs.QueryDefs.Delete "Find Employees"
dbs.Close
End Sub