How to use DSum to create a running sum on a form in Access 2002 (325237)



The information in this article applies to:

  • Microsoft Access 2002

This article was previously published under Q325237
For a Microsoft Access 97 version of this article, see 103183.
For a Microsoft Access 2000 version of this article, see 210495.

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

This article applies only to a Microsoft Access database (.mdb).

IN THIS TASK

SUMMARY

Reports have a RunningSum property. Forms do not have a RunningSum property. This article describes how to create a running sum on a form.

back to the top

Example of a Running Sum


To create a running sum on a form, the underlying table or query must have a unique and a sequential ID field, such as an AutoNumber or a Date/Time time stamp field. If your table does not have such a field, you can open the table in Design view and then add a new field to create one.

The following example uses the sample database Northwind.mdb to show you how to create a text box on a form that sums the freight amount for the current customer order and all earlier customer orders:
  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Create the following new form based on the Orders table.

    NOTE: To improve readability, the underscore (_) is used in the following example as a line-continuation character. Remove the underscore from the end of the line when you are re-creating this example.
          Form:  Form1
          --------------------
          RecordSource: Orders
    
          Text Box: Order ID
             ControlSource: OrderID
          Text Box: Total
             ControlSource: =DSum("[Freight]","Orders",_
                                  "[OrderID] <= Forms![Form1]![OrderID]")
    						
    The DSum() function calculates the sum of all freight charges on orders with an Order number less than or equal to the order number that currently appears. For this reason, you must have a unique and a sequential ID field.
  3. View the form in Form view. Note that the Total text box accumulates total freight charges for each order as you move from record to record.
back to the top

Modification Type:MajorLast Reviewed:8/2/2004
Keywords:kbfunctions kbhowto KB325237