ACC: DFirst() and DLast() Functions Return Unexpected Records (109380)
The information in this article applies to:
- Microsoft Access 1.0
- Microsoft Access 1.1
- Microsoft Access 2.0
This article was previously published under Q109380
Moderate: Requires basic macro, coding, and interoperability skills.
SYMPTOMS
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.
CAUSE
DFirst() 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.
RESOLUTION
The 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
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 5/6/2003 |
---|
Keywords: | kbusage KB109380 |
---|
|