ACC: DFirst() and DLast() Functions Return Unexpected RecordsLast reviewed: April 2, 1997Article ID: Q109380 |
The information in this article applies to:
SYMPTOMSModerate: Requires basic macro, coding, and interoperability skills. The DFirst() and DLast() functions do not return the first and last records of the specified domain as you expect. If the domain is a query, the DFirst() and DLast() functions appear to ignore the sort order of the query. If the domain is a table, DFirst() and DLast() appear to ignore the order of the current index or primary key.
CAUSEDFirst() and DLast() ignore sort orders and indexes, even Primary Keys. These functions are intended to return data from the first or last record entered into the table, not the first or last record in a given sort order. Microsoft Access online Help incorrectly states:
DFirst and DLast return values from the first and last occurrence according to the order of records in domain. If domain is an indexed table, the order follows the current index. Otherwise, the order follows the actual order of the records. RESOLUTIONThe following two sample Access Basic functions, GetFirst() and GetLast(), can be used in place of the DFirst() and DLast() functions to return the first and last records in the sorted domain as you expect. NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.
Option Explicit Function GetFirst (Expr$, Domain$, Criteria$) Dim MyDB As Database Dim MyDyna As Dynaset On Error GoTo Err_GetFirst Set MyDB = CurrentDB() Set MyDyna = MyDB.CreateDynaset(Domain$) If Len(Criteria$) > 0 Then MyDyna.Filter = Criteria$ Set MyDyna = MyDyna.CreateDynaset() End If MyDyna.MoveFirst GetFirst = MyDyna(Expr$) Bye_GetFirst: Exit Function Err_GetFirst: GetFirst = Null Resume Bye_GetFirst End Function Function GetLast (Expr$, Domain$, Criteria$) Dim MyDB As Database Dim MyDyna As Dynaset On Error GoTo Err_GetLast Set MyDB = CurrentDB() Set MyDyna = MyDB.CreateDynaset(Domain$) If Len(Criteria$) > 0 Then MyDyna.Filter = Criteria$ Set MyDyna = MyDyna.CreateDynaset() End If MyDyna.MoveLast GetLast = MyDyna(Expr$) Bye_GetLast: Exit Function Err_GetLast: GetLast = Null Resume Bye_GetLast End Function STATUSThis behavior is by design.
MORE INFORMATION
Steps to Reproduce Behavior
|
Keywords : ExrOthr kbusage
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |