Solved

DLast not working correctly

Posted on 2014-11-14
7
183 Views
Last Modified: 2014-11-20
I'm using this code to get the last record in a table:

=DLast("[StatinsMed]","Treatment","[MemberID] = " & [Forms]![frmMemberMaster]![txtMemberID])

But it isn't working.  It's actually giving me the middle of 3 record values.

????
0
Comment
Question by:SteveL13
7 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40443701
DLast and DFirst really only return "some value".

You should use DMax or DMin for the highest or lowest value.

/gustav
0
 

Author Comment

by:SteveL13
ID: 40443820
I guess I've been living in a cave.  I would think that DLast would give me the last records value.  DMax and DMin do not work either.
0
 
LVL 18

Expert Comment

by:bonjour-aut
ID: 40444199
asuming that the memberID is a string:

DLast("[StatinsMed]","Treatment","[MemberID] = '" & [Forms]![frmMemberMaster]![txtMemberID]) & "'"

as string needs to be wrapped in single quotes
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 18

Expert Comment

by:bonjour-aut
ID: 40444200
withou the qoutes, the domain function filter is not valid and acts like  DLast("[StatinsMed]","Treatment","")

sorry i misstyped my first comment. it should read like:

DLast("[StatinsMed]","Treatment","[MemberID] = '" & [Forms]![frmMemberMaster]![txtMemberID] & "'")
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40444332
>  I would think that DLast would give me the last records value.
You can use the DFirst and DLast functions to return a random record from a particular field in a table or query when you simply need any value from that field. Use the DFirst and DLast functions in a macro, module, query expression, or calculated control on a form or report.
> DMax and DMin do not work either.
Then you will have to define the criteria for "last record".
It could be the newest value of a timestamp field.

/gustav
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40446550
The help entry -  http://office.microsoft.com/en-001/access-help/dfirst-dlast-functions-HA001228823.aspx explains what you get with DFirst() and DLast() and it isn't what you think it should be.  I'm not even sure what the point of having these two function is since they get lots of folks in trouble and don't seem to deliver useful results.

Keep in mind that in English, First and Last refer to ordinal position in a set.  In a relational database, tables/queries are unordered sets so First and Last have no fixed meaning since from one query to the next, First and Last could be different.  That's why the help entry says you get a random result.

As Gus suggested, tell us what you are really after and we'll see how to make Min/Max solve the problem.
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40454037
If you want order, you'll need a recordset
I suspect you are using this as a ControlSource or in a query -- so you need a public function.
In a module put

Option Compare Database
Option Explicit

Public Function LastStatinsMed as String
Dim rs as recordset

set rs = CurrentDb.OpenRecordset("Select [StatinsMed] from Treatment  where [MemberID] = " & [Forms]![frmMemberMaster]![txtMemberID] & " Order by ThePrimaryKey Desc, dbOpenDynaset, dbSeeChanges)
'whatever your primary key may be

LastStatinsMed = rs!StatinsMed
End Function

Open in new window


You can then give your control a source, or use as a query parameter
=LastStatinsMed()
instead of
=DLast("[StatinsMed]","Treatment","[MemberID] = " & [Forms]![frmMemberMaster]![txtMemberID])

To get decent performance you should avoid the use of Domain Aggregate functions in favor of similar code in all cases.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question