Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DLast not working correctly

Posted on 2014-11-14
7
Medium Priority
?
240 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 39

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 2000 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

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!

Question has a verified solution.

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

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…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

721 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