Article ID: 101081
Article Last Modified on 1/18/2007
182568 ACC97: Microsoft Access 97 Sample Queries Available in Download Center
=DLookUp("[Field]","Table","[ID]=Forms![Form1]![ID]-1")
To obtain a value from a field in the next record, type the following line
for the text box's ControlSource property:
=DLookUp("[Field]","Table","[ID]=Forms![Form1]![ID]+1")
=DLookUp("[Field]","Table","[ID]=Reports![Report1]![ID]-1")
To obtain a value from a field in the next record, type the following line
for the text box's ControlSource property:
=DLookUp("[Field]","Table","[ID]=Reports![Report1]![ID]+1")
Expr1: DLookUp("[Field1]","Table1","[ID]=" & [ID]-1)
To obtain a value from a field in the next record, type the following line
in the Field row of the query grid:
Expr1: DLookUp("[Field1]","Table1","[ID]=" & [ID]+1)
NOTE: In these sample expressions, the "-1" and "+1" indicate the previous
and next records. When the current record is the first record in the
recordset, the "-1" returns a Null value because there is no previous
record. Likewise, when the current record is the last record in the
recordset, the "+1" returns a Null. If you want to return a value from a
record other than the next or previous one, you can specify a different
number, for example, "-3" for the third previous record.
Option Explicit
'*************************************************************
' FUNCTION: PrevRecVal()
' PURPOSE: Retrieve a value from a field in the previous form
' record.
' PARAMETERS:
' F - The form from which to get the previous value.
' KeyName - The name of the form's unique key field.
' KeyValue - The current record's key value.
' FieldNameToGet - The name of the field in the previous
' record from which to retrieve the value.
' RETURNS: The value in the field FieldNameToGet from the
' previous form record.
' EXAMPLE:
' =PrevRecVal(Form,"ID",[ID],"OdometerReading")
'**************************************************************
Function PrevRecVal (F As Form, KeyName As String, KeyValue, _
FieldNameToGet As String)
Dim RS As Recordset
On Error GoTo Err_PrevRecVal
' The default value is zero.
PrevRecVal = 0
' Get the form recordset.
Set RS = F.RecordsetClone
' Find the current record.
Select Case RS.Fields(KeyName).Type
' Find using numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & KeyName & "] = " & KeyValue
' Find using date data type key value?
Case DB_DATE
RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
' Find using text data type key value?
Case DB_TEXT
RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "ERROR: Invalid key field data type!"
Exit Function
End Select
' Move to the previous record.
RS.MovePrevious
' Return the result.
PrevRecVal = RS(FieldNameToGet)
Bye_PrevRecVal:
Exit Function
Err_PrevRecVal:
Resume Bye_PrevRecVal
End Function
'*************************************************************
' FUNCTION: NextRecVal()
' PURPOSE: Retrieve a value from a field in the next form
' record.
'**************************************************************
Function NextRecVal (F As Form, KeyName As String, KeyValue, _
FieldNameToGet As String)
Dim RS As Recordset
On Error GoTo Err_NextRecVal
' The default value is zero.
NextRecVal = 0
' Get the form recordset.
Set RS = F.RecordsetClone
' Find the current record.
Select Case RS.Fields(KeyName).Type
' Find using numeric data type key value?
Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, _
DB_DOUBLE, DB_BYTE
RS.FindFirst "[" & KeyName & "] = " & KeyValue
' Find using date data type key value?
Case DB_DATE
RS.FindFirst "[" & KeyName & "] = #" & KeyValue & "#"
' Find using text data type key value?
Case DB_TEXT
RS.FindFirst "[" & KeyName & "] = '" & KeyValue & "'"
Case Else
MsgBox "ERROR: Invalid key field data type!"
Exit Function
End Select
' Move to the next record.
RS.MoveNext
' Return the result.
NextRecVal = RS(FieldNameToGet)
Bye_NextRecVal:
Exit Function
Err_NextRecVal:
Resume Bye_NextRecVal
End Function
Table: Mileage Log
-----------------------------------------------------------------
Field Name: ID
Data Type : AutoNumber (or Counter in Access 2.0 or earlier)
Indexed: Yes (No Duplicates)
Field Name: Date
Data Type : Date/Time
Field Name: Odometer
Data Type : Number
FieldSize: Double
Field Name: Gallons
Data Type : Number
FieldSize: Double
Primary Key: ID
ID Date Odometer Gallons
---------------------------------
1 6/21/94 77917.8 10.2
2 6/25/94 78254.7 9.6
3 6/30/94 78582.3 10
4 7/5/94 78918.4 10.4
5 7/10/94 79223.4 9.4
Text Box 1
-----------------------------------------------------
Name: PrevOdometer
ControlSource: =PrevRecVal(Form,"ID",[ID],"Odometer")
Format: Fixed
Text Box 2
-----------------------------------------------------------------
Name: MilesDriven
ControlSource: =iif([PrevOdometer]=0,0,[Odometer]-[PrevOdometer])
Format: Fixed
Text Box 3
---------------------------------------
Name: MPG
ControlSource: =[MilesDriven]/[Gallons]
Format: Fixed
Date Odometer Gallons PrevOdometer MilesDriven MPG
-------------------------------------------------------------
6/21/94 77917.8 10.2 0 0 0
6/25/94 78254.7 9.6 77917.80 336.90 35.09
6/30/94 78582.3 10.0 78254.70 327.60 32.76
7/05/94 78918.4 10.4 78582.30 336.10 32.32
7/10/94 79223.4 9.4 78918.40 305.00 32.45136127 ACC: Fill Record w/Data from Prev. Record Automatically (95/97)
Additional query words: next running sum
Keywords: kbfaq kbhowto kbprogramming kbusage KB101081