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