Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Sample Function to Create a Running Sum on a Form


View products that this article applies to.

Summary

This article shows you how to create a sample user-defined function to compute a running sum on a form.

↑ Back to the top


More information

One way to compute a running sum on a form is to use the DSum() function. For additional information about how to use the DSum() function to compute a running sum on a form, please see the following article in the Microsoft Knowledge Base:
210495 ACC2000: How to Use DSum to Create a Running Sum on a Form
Using the method demonstrated in this article has the following advantages over using the DSum() function:
  • The method demonstrated in this article does not require a sequential ID field. A required sequential ID field limits you to computing a running sum using a single ordering of your records.
  • Using the method demonstrated in this article is significantly faster than using the DSum() function to compute a running sum.

How to Create and Use the RunSum() Function

Microsoft 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.

  1. Open the sample database Northwind.mdb. Create a new module, and type the following line in the Declarations section if it is not already there:
    Option Explicit
  2. Type the following code in the module:
    Function RunSum (F As Form, KeyName As String, KeyValue, _
      FieldToSum As String)
    '***********************************************************
    ' FUNCTION: RunSum()
    ' PURPOSE:  Compute a running sum on a form.
    ' PARAMETERS:
    '    F        - The form containing the previous value to
    '               retrieve.
    '    KeyName  - The name of the form's unique key field.
    '    KeyValue - The current record's key value.
    '    FieldToSum - The name of the field in the previous
    '                 record containing the value to retrieve.
    ' RETURNS:  A running sum of the field FieldToSum.
    ' EXAMPLE:  =RunSum(Form,"ID",[ID],"Amount")
    '***********************************************************
       Dim RS As DAO.Recordset
       Dim Result
    
       On Error GoTo Err_RunSum
    
       ' 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!"
             GoTo Bye_RunSum
       End Select
    
       ' Compute the running sum.
       Do Until RS.BOF
          Result = Result + RS(FieldToSum)
    
          ' Move to the previous record.
          RS.MovePrevious
       Loop
    
    Bye_RunSum:
       RunSum = Result
       Exit Function
    
    Err_RunSum:
       Resume Bye_RunSum
    
    End Function
  3. Create the following new query based on the Orders table and the Order Subtotals query. Save the query as qryOrders:
       Query: qryOrders
       ------------------------------------------------------
       Type: select query
       Join: Orders.[OrderID] <->; [Order Subtotals].[OrderID]
    
       Field: OrderID
          Table: Orders
          Sort: Ascending
       Field: Subtotal
          Table: Order Subtotals
  4. Use the Form Wizard to create a new form based on the qryOrders query. Include both fields on the form. On the next screen click Tabular.
  5. Add a text box with the following properties to the new form:
       Name: RunningSum
       ControlSource: =RunSum([Form],"OrderID",[OrderID],"Subtotal")
       Format: Currency
  6. Save the form and then view it in Form view.
  7. Select different records using the record selector. Note that the RunningSum field shows an accumulated total based on the Subtotal field.

↑ Back to the top


Keywords: KB210338, kbusage, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 210338
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 385