Solved

DLast not working correctly

Posted on 2014-11-14
7
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
asuming that the memberID is a string:

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

as string needs to be wrapped in single quotes
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:bonjour-aut
Comment Utility
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
Comment Utility
>  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 34

Expert Comment

by:PatHartman
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now