Table of contents
TOC
Collapse the table of content
Expand the table of content

First, Last Functions (Microsoft Access SQL)

office 365 dev account|Last Updated: 4/3/2018
|
3 Contributors

Applies to: Access 2013 | Access 2016

In this articleSyntaxRemarks ExampleAbout the Contributors

Return a field value from the first or last record in the result set returned by a query.

Syntax

First(expr)

Last(expr)

The expr placeholder represents a string expression identifying the field that contains the data you want to use or an expression that performs a calculation using the data in that field. Operands in expr can include the name of a table field, a constant, or a function (which can be either intrinsic or user-defined but not one of the other SQL aggregate functions).

Remarks

The First and Last functions are analogous to the MoveFirst and MoveLast methods of a DAO Recordset object. They simply return the value of a specified field in the first or last record, respectively, of the result set returned by a query. Because records are usually returned in no particular order (unless the query includes anORDER BY clause), the records returned by these functions will be arbitrary.

Link provided by:Community Member Icon TheUtterAccess community |About the Contributors

Example

This example uses the Employees table to return the values from the LastName field of the first and last records returned from the table.

This example calls the EnumFields procedure, which you can find in the SELECT statement example.

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 

The next example compares using the First and Last functions with simply using the Min and Max functions to find the earliest and latest birth dates of Employees.

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 

About the Contributors

UtterAccess is the premier Microsoft Access wiki and help forum. Click here to join.

ACCESS SUPPORT RESOURCES
Access for developers forum on MSDN
Access help on support.office.com
Access help on answers.microsoft.com
Search for specific Access error codes on Bing
Access forums on UtterAccess
Access wiki on UtterAcess
Access developer and VBA programming help center (FMS)
Access posts on StackOverflow

© 2018 Microsoft