首先,Microsoft Access SQL) (Last 函数

适用于:Access 2013 | Access 2016

返回在查询所返回的结果集中的第一个或者最后一个记录的字段值。

语法

第一 个 ( expr )

上次 ( expr )

expr 占位符代表一个字符串表达式(它标识了包含您要使用的数据的字段),或者代表一个使用该字段中的数据执行计算的表达式。 expr 中的操作数可以包括表字段、常量或者函数(可以是固有函数或用户定义的函数,但不能是其他 SQL 聚合函数)的名称。

备注

FirstLast 函数类似于 DAO Recordset 对象的 MoveFirstMoveLast 方法。 它们分别返回查询所返回的结果集的第一个或最后一个记录中指定字段的值。 因为记录通常以非特定顺序返回(除非查询中包含了 ORDER BY 子句),所以这些函数返回的记录是任意的。

示例

本例使用 Employees 表返回从该表返回的第一条记录和最后一条记录的 LastName 字段中的值。

以下示例调用 EnumFields 过程,您可以在 SELECT 语句示例中找到该过程。

Sub FirstLastX1() 
 
    Dim dbs As Database, rst As Recordset 
 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
     
    ' Return the values from the LastName field of the  
    ' first and last records returned from the table. 
    Set rst = dbs.OpenRecordset("SELECT " _ 
        & "First(LastName) as First, " _ 
        & "Last(LastName) as Last FROM Employees;") 
     
    ' 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 
 
    dbs.Close 
 
End Sub 

下一个示例将 FirstLast 函数的结果与 MinMax 函数的结果进行比较,以查找雇员的最早和最晚出生日期。

Sub FirstLastX2() 
 
    Dim dbs As Database, rst As Recordset 
 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
     
    ' Find the earliest and latest birth dates of 
    ' Employees. 
    Set rst = dbs.OpenRecordset("SELECT " _ 
        & "First(BirthDate) as FirstBD, " _ 
        & "Last(BirthDate) as LastBD FROM Employees;") 
     
    ' 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 
     
    Debug.Print 
 
    ' Find the earliest and latest birth dates of 
    ' Employees. 
    Set rst = dbs.OpenRecordset("SELECT " _ 
        & "Min(BirthDate) as MinBD," _ 
        & "Max(BirthDate) as MaxBD FROM Employees;") 
     
    ' 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 
 
    dbs.Close 
 
End Sub 

关于参与者

社区成员图标UtterAccess 社区是首屈一指的 Microsoft Access Wiki 和帮助论坛。

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。