ACC: How to Compare a Field to a Field in a Prior Record

Last reviewed: April 2, 1997
Article ID: Q120273
The information in this article applies to:
  • Microsoft Access versions 2.0, 7.0, 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes how to compare the values in a record with the values in another record using a subquery. In order to use the method described in this article, the table you are using must have one or more fields that can be compared using the GREATER THAN (>) or LESS THAN (<) operator.

MORE INFORMATION

The following example demonstrates how to calculate miles per gallon by subtracting a recorded mileage from the current mileage, and then dividing by the number of gallons of gasoline recorded in the current record:

  1. Create the following new table, and then save it as MileageRecord. Do not create a primary key for the table:

          Table: MileageRecord
          -----------------------
          Field Name: Date
          Data Type: Date/Time
          Field Name: Mileage
          Data Type: Number
          Field Size: Single
          Field Name: Gallons
          Date Type: Number
          Field Size: Double
    

  2. View the table in Datasheet view, and enter the following records in the table:

           Date      Mileage   Gallons
           ---------------------------
           7/08/94   12340     14.8
           7/13/94   12700     12.6
           7/18/94   13090     13.7
           7/25/94   13425     11.9
    
    

  3. Create a new, blank query based on the MileageRecord table. Add the Date and Mileage fields to the query grid.

  4. Click the Properties button on the toolbar to view the property sheet, and then select the title bar of the MileageRecord table. Set the table's Alias property to Mile1.

  5. Enter the following expression in the third column in the query grid.

    NOTE: In the following sample expression, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this expression.

          PrevMileage: (Select Max(Mileage) from MileageRecord _
          Where Mileage < Mile1.[Mileage])
    

    This expression is a subquery that finds the highest mileage in the MileageRecord table that is less than the mileage of the current record.

  6. Enter the following expression in the fourth column in the query grid:

          Elapsed: [Mileage] - [PrevMileage]
    

    This field calculates the difference between the current and previous mileage entries.

  7. Enter the following expression in the fifth column in the query grid:

          MPG: ([Mileage] - [PrevMileage]) / Gallons
    

    This field calculates the miles per gallon.

  8. Run the query. Note that you receive the following results:

          Date      Mileage   PrevMileage   Elapsed   MPG
          ------------------------------------------------------------
          7/08/94   12340
          7/13/94   12700     12340         360       28.5714285714286
          7/18/94   13090     12700         390       28.4671532846715
          7/25/94   13425     13090         335       28.1512605042017
    
    

REFERENCES

For more information about subqueries, search the Help Index for "subqueries," or ask the Microsoft Access 97 Office Assistant.


Additional query words: differential calculus
Keywords : kbusage QrySqlvw
Version : 2.0 7.0 97
Platform : WINDOWS
Hardware : X86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.