Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-查询/SQL语句

全面掌握MS ACCESS SQL(44)

时 间:2018-01-25 16:20:18
作 者:Big Young   ID:252  城市:襄阳
摘 要:    用PARAMETERS声明参数查询的完整语法。
正 文:

第十四章 用PARAMETERS声明参数查询

参数查询是指某些情况下,需要创建可以使用多次,但每次使用不同值的查询。参数,也叫参变量,是一个变量。查询参数是指使用函数或方法进行查询操作时,函数或方法要使用的参数。一般进行查询操作时,查询参数不同,查询返回结果是不同,即参数决定了查询结果。查询参数使用最多地方是在数据库系统中。

第一节 用PARAMETERS声明参数查询的完整语法

一、PARAMETERS声明的完整语法

ACCESS SQL中,用PARAMETERS来声明参数查询,其完整的语法如下:

PARAMETERS name datatype [, name datatype [, ...]]

PARAMETERS声明包含以下部分:

部分

说明

name

参数的名称。该名称被赋给 Parameter 对象的 Name 属性,并且用来在 Parameters 集合中标识该参数。可以将 name 作为应用程序运行查询时在对话框中显示的字符串。请用方括号 ([ ]) 将包含空格或标点的文本括起来。例如,[Low price] [Begin report with which month?] 都是有效的 name 参数。

datatype

主要 Microsoft Access SQL 数据类型或其同义词之一。

二、几点注解说明

对于定期运行的查询,可以通过PARAMETERS声明来创建一个参数查询。参数查询能够自动处理查询条件更改。若使用参数查询,在每次查询运行时代码都需要提供参数。

PARAMETERS声明是可选的,但如果包含它,应将它置于任何其他语句(包括Select语句)之前。

如果声明包含了多个参数,请用逗号分隔它们。以下的示例里包含了两个参数:

PARAMETERS [Low price] Currency, [Beginning date] DateTime;

WhereHAVING子句中可以使用name参数,不能使用datatype参数。以下的示例中要求提供两个参数,然后将该条件应用于Orders表的记录中:

PARAMETERS [Low price] Currency,

[Beginning date] DateTime;

Select orderID, orderAmount

FROM orders

Where orderAmount > [Low price]

AND orderDate >= [Beginning date];

三、VBA编程示例

本示例要求用户提供职务,然后使用该职务作为查询条件。

本示例调用 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



Access软件网官方交流QQ群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助