previous record value

olerebel

New member
Local time
Today, 06:45
Joined
Jul 16, 2006
Messages
9
Is there a way I can grab the previous record's value without using dlookup? I am trying to create a running total in a datasheet view. e.g.

Date Credit Debit Run. Tot.
6/1/06 $25 $0 $25
6/4/06 $30 $0 $55
6/9/06 $0 $50 $5

I could do it by using an autonumber but the data is sorted by date and not by the order in which it was entered. Suggestions? :o
 
Do a search on open recordset; you can use a query as a recordset to pass along the data.
 
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 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 = Round2CB(result + rs(FieldToSum))

' Move to the previous record.
rs.MovePrevious
Loop

Bye_RunSum:
RunSum = result

Exit Function

Err_RunSum:
Resume Bye_RunSum

End Function
 
almost there

Rich, thanks for the code. Unfortunaltly I can't get it to work with what I am trying to do. I am trying to run it on a form that is in a datasheet view. e.g.
Credit Debit Run.Total
$5 $0 $5
$9 $0 $14
$5 $0 $19
$0 $9 $10
$0 $6 $4

suggestions?
 

Users who are viewing this thread

Back
Top Bottom