Solved

DLast not working correctly

Posted on 2014-11-14
7
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 50

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 50

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 36

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

740 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