Article ID: 210504 - Last Review: June 23, 2005 - Revision: 3.0 ACC2000: Referring to a Field in the Previous Record or Next RecordThis article was previously published under Q210504 Advanced: Requires expert coding, interoperability, and multiuser skills.
This article applies only to a Microsoft Access database (.mdb). On This PageSUMMARY
This article describes two methods that you can use to obtain values from
the previous or the next record for use in calculations. The first method
uses the DLookup() function in an expression; the second method uses two user-defined functions.
NOTE: You can see a demonstration of the technique that is used in this article in the sample file Qrysmp00.exe. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base: 207626
(http://support.microsoft.com/kb/207626/EN-US/
)
ACC2000: Access 2000 Sample Queries Available in Download Center
MORE INFORMATIONMethod 1: Using DLookup()NOTE: This technique assumes that you have a table with an ID field of a Number data type, and that the ID values are not missing any number (or numbers) in sequential order. If your table does not meet these criteria, you should use the "Using Code" method described later in this article.You can use the following sample DLookup() expressions to obtain values from a field in the previous or next record of a form, a report, or a query. NOTE: You must insert the correct data field, table, form, report, and ID field names in the following sample expressions.
In a FormTo obtain a value from a field in the previous record, type the following line for the text box's ControlSource property:=DLookUp("[MyField]","MyTable","[ID]=Forms![MyForm]![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("[MyField]","MyTable","[ID]=Forms![MyForm]![ID]+1") In a ReportTo obtain a value from a field in the previous record, type the following line for the text box's ControlSource property:=DLookUp("[MyField]","MyTable","[ID]=Reports![MyReport]![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("[MyField]","MyTable","[ID]=Reports![MyReport]![ID]+1") In a QueryTo obtain a value from a field in the previous record, type the following line in the Field row of the query grid:Expr1: DLookUp("[MyField]","MyTable","[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("[MyField]","MyTable","[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.
Method 2: Using CodeMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft DAO 3.6 Object Library check box is selected.To retrieve a value in a field from the previous or next record in a form by using code, follow these steps:
REFERENCESFor additional information about entering data automatically into a form by using values from the previous record, click the article number below
to view the article in the Microsoft Knowledge Base:
210236
(http://support.microsoft.com/kb/210236/EN-US/
)
ACC2000:Fill Record with Data from Previous Record Automatically
| Article Translations
|
Back to the top
