ACC: DFirst() and DLast() Functions Return Unexpected RecordsID: Q109380
|
Moderate: 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.
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.
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
This behavior is by design.
Keywords : kbusage ExrOthr
Version : 1.0 1.1 2.0
Platform : WINDOWS
Issue type :
Last Reviewed: March 30, 1999