The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills. This article applies only to a Microsoft Access database (.mdb). SYMPTOMS
In the Totals row of the query design grid, you can choose the First() or Last() function to return the value of a specified field in the first or last record, respectively, of the result set returned by the query. However, these functions may appear to return arbitrary values because the records are returned in an order that may not be apparent.
CAUSEThe First(), Last(), DFirst(), and DLast() functions ignore sort orders, indexes, and primary keys. These functions return the first or last undeleted record based on the order in which the records were entered into the table, not the first or last record in a given sort order. RESOLUTIONMicrosoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspIn some cases, compacting the database may return the results that you want. To ensure that the results will always be consistent, use one or more of the following procedures. In order to obtain the expected first and last records, you should organize the recordset data in a predictable sequence. You can do this by using one of the following methods. CAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database. Method 1If you are looking for the highest and lowest values in the result set, use the Min() and Max() functions in place of the First() or DFirst() and Last() or DLast() functions. For example, to get the highest and lowest freight in the Orders table, follow these steps:
Method 2Another way to get the first and last records in a result set is to sort the query in ascending order (or descending order if you want the last record), and to set the TopValues property to 1. For example, to get the last CategoryID in the Categories table, follow these steps:
Method 3A third way to get the first and last record in a result set is to open a recordset based on a query in DAO, and then to use the MoveFirst or MoveLast methods to get the first or last record.To display the first and last Order date in the Orders table of the sample database Northwind.mdb, follow these steps:
MORE INFORMATIONThe "Remarks" section of the Help File topic, "First, Last Functions" incorrectly states that unless the query includes an ORDER BY clause, the order of records returned will be arbitrary. The correct information is that the order of records returned by the First() and Last() functions is not affected by the ORDER BY clause. Additional query words:
Keywords : kbdta QryOthr |
Last Reviewed: July 6, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |