Joan
One simple way to do this is to use a DMax and then a DLookup;
Assumes a Public variable declared in a global module, say a double
Public dblSMargin As Double
Dim lngID As Long
lngID = DMax("[SalesMarginID]", "[SalesMargin]")
dblSMargin = DLookup("[SMFactor]",
"[SalesMargin]","[SalesMarginID] = " & lngID)
HTH
Andy
John,
By last record, I mean the last record entered into the table. This
is a SalesMargin table where the database can retrieve the current
sales margin to add to the cost of products( or in other words the
'last entered'). By retrieving the last entered sales margin from
the table the user does not have to set a variable to this value
each time they open the database. What is the best way to achieve
this?
Note, though, that this approach only works if there is a SalesMarginID
field that can be relied on to be always ascending (so that the record
with the maximum ID is the most recent one). An ascending autonumber
would work for this, provided that (a) the database will bever be
replicated, which causes autonumbers to go random, and (b) no glitch
occurs that causes the database to start assigning negative autonumbers
(which has been known to happen). For safety's sake, you really
shouldn't make any assumptions about the value of autonumbers.
If you have a non-autonumber SalesMarginID field and you use code to
assign your own, ascending values to it, then you *can* rely on it to be
in ascending sequence and Andy's suggested approach is a good one. If
not, then the only reliable way to retrieve the most recently entered
record in the table is to have a date-time field in the table with a
default value of Now(). Then you look up the record with the maximum
value in that date-time field. Even this approach won't work if it's
possible for there to be two records created at the same moment, but my
guess from Joan's description is that that won't happen.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)