Public Function fnBalance(ByVal ID As Long) As Double
' ID can be:
' -1 release the recordset
' 0 requery the recordset
' any number the actual id number
Static rs As DAO.Recordset 'rs is preserved when the db is open and will be closed automatically when you the db
Dim result As Double
If ID = -1 Then
If Not (rs Is Nothing) Then
rs.Close
Set rs = Nothing
Exit Function
End If
End If
If rs Is Nothing Then
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblChecking ORDER BY TransDate, ID;", dbOpenSnapshot, dbReadOnly)
End If
If ID = 0 Then
rs.Requery
Exit Function
End If
With rs
.FindFirst "ID = " & ID
Do Until .BOF
result = result + Nz(!Credit, 0) - Nz(!Debit, 0)
.MovePrevious
Loop
End With
fnBalance = result
End Function