Home > Language Reference > Classes > Recordset

Sum Function

Returns the sum of a given field over the whole recordset.

Syntax

Public Function Sum(ByRef sFieldName As String) As Double

ParameterDescription
sFieldNameName of the field on which to perform the computation.

Remarks

The Sum function computes the sum of the field specified by the sFieldName parameter over the whole recordset. If the recordset was opened using OpenRecordset, the computation is performed over the records that match the specified query. On the other hand, if the recordset was opened using OpenTable, the computation is perfomed over all the records in the table.

For example, the following code displays the total sales of books published in 2000:

Private Sub Button1_Click()
  Dim db as New tblBooks

  db.OpenRecordset "PubDate=2000", hbModeOpenExisting+hbModeReadOnly
  MsgBox db.Sum("Sales")
  db.Close
End Sub

The cursor position is not modified by a call to this function, nor the value of the various fields of the current record, thus it can be used while enumerating records.

Note that this function can only operate on fields whose type is Byte, Integer, Long, Single, and Double. If the sFieldName parameter specifies another type of field, a runtime error occurs. If the recordset does not contain any record, the returned value is zero.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0-
Windows CEWindows CE 3.0-