Solved

DLast not working correctly

Posted on 2014-11-14
7
221 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 51

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 51

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 38

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

623 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