DLAST function, results not showing (1 Viewer)

deanvilar

Registered User.
Local time
Today, 03:30
Joined
Mar 27, 2013
Messages
63
I used Dlast function to determine last record of such Field as reference, but as the fields are apart from the record it does not return the last record why is that so?

attached is a sample of the records

Code:
dim rYear as String
 rYear = Nz(DLast("returnedYear", "documentLog", "fileCode LIKE'" & Me.txtFileCode & "*'"), "")
 

Attachments

  • CAM0002 SAMPLE.jpg
    CAM0002 SAMPLE.jpg
    94.3 KB · Views: 114

Solo712

Registered User.
Local time
Today, 06:30
Joined
Oct 19, 2012
Messages
828
I used Dlast function to determine last record of such Field as reference, but as the fields are apart from the record it does not return the last record why is that so?

attached is a sample of the records

Code:
dim rYear as String
 rYear = Nz(DLast("returnedYear", "documentLog", "fileCode LIKE'" & Me.txtFileCode & "*'"), "")

Hi Dean,
there is no guarantee that DLast will return the last record. It will return the last value for the specified field ( as defined by the criteria). So, in your example you will get the last returned year for the txtFileCodes fitting the criteria. There may be x number of records with the same returned year by DLast.

Personally, I have yet to find the purpose DLast. If I want to retrieve the last record, the most reliable indicator would the highest PK value found with DMax, similarly for fields sortable by dates, etc.

Best,
Jiri
 

deanvilar

Registered User.
Local time
Today, 03:30
Joined
Mar 27, 2013
Messages
63
Hello Jiri,

thanks for the heads up ... how would I do that PK found with DMax? any tips?
 

Solo712

Registered User.
Local time
Today, 06:30
Joined
Oct 19, 2012
Messages
828
Hello Jiri,

thanks for the heads up ... how would I do that PK found with DMax? any tips?

Assuming that DocLogNo is the primary key then you would do
Code:
 = Nz(DMax("DocLogNo", "documentLog"))
You can create criteria the same way you did with the DLast. So, eg. if you were searching for Villar the DMax would return '00000007', if for Guanzon the '00000006', etc.

Best,
Jiri
 

deanvilar

Registered User.
Local time
Today, 03:30
Joined
Mar 27, 2013
Messages
63
I got it!!!! thanks for the idea Jiri....ur d man!

I used Dmax on PK to determine the last fileCode field and then compared the PK on the succeeding arguments... damn right! thanks
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:30
Joined
Jan 20, 2009
Messages
12,853
You are still using DLast despite being advised not to do so.

DFirst and DLast return the first or last matching record that Access comes across. This does not necessarily represent any particular order since the database engine can insert new records anywhere in the table.

If you want the last record in a particular order you must use DMax on a field that is in the entry sequence.
 

deanvilar

Registered User.
Local time
Today, 03:30
Joined
Mar 27, 2013
Messages
63
@Galaxiom that was my old codes, I tried using Dmax on PK and it worked perfectly fine... thanks
 

Users who are viewing this thread

Top Bottom