this probably needs more work, but this works on the example data you provided
The Query
Code:
SELECT TranData.Security, TranData.TranDate, fTtlHolding([Security],[Trantype],[Quantity]) AS TtlHolding, facbChange([security],[trantype],[quantity],[price],[commission]) AS ACBChange, facb([security],[trantype],[quantity],[price],[commission]) AS ACB
FROM TranData
ORDER BY TranData.Security, TranData.TranDate
Uses 3 UDF's here - paste into a new module
Note the use of the static variable declaration which accumulates the values. The problem with the static values is they will keep on accumulating - click on a query row and the values will change. So, strongly recommend you make the above sql a recordsource to a form where you have disabled editing etc so you cannot select a row - alternatively modify the query to be a make table or append query. Manually, you can reset the statics by closing the db or by in the vba editor typing 'end' (no quotes) in the immediate window.
Code:
Function fACB(Security As String, BS As String, Quantity As Long, Price As Currency, Commission As Currency) As Currency
Static ACBTotal As Currency
Static oldSecurity As String
Static QtyTotal As Long
If Security = oldSecurity Then 'reset for next security
ACBTotal = 0
QtyTotal = 0
oldSecurity = Security
End If
Select Case BS
Case "Buy"
ACBTotal = ACBTotal + ((Quantity * Price) + Commission)
QtyTotal = QtyTotal + Quantity
Case "Sell"
ACBTotal = ACBTotal / QtyTotal * (QtyTotal - Quantity)
QtyTotal = QtyTotal - Quantity
Case Else
MsgBox "Buy/Sell not specified"
End Select
fACB = ACBTotal
End Function
Function fACBChange(Security As String, BS As String, Quantity As Long, Price As Currency, Commission As Currency) As Currency
Static ACBTotal As Currency
Static oldSecurity As String
Static QtyTotal As Long
Dim ACBChange As Currency
If Security = oldSecurity Then 'reset for next security
ACBTotal = 0
QtyTotal = 0
oldSecurity = Security
End If
Select Case BS
Case "Buy"
ACBChange = ((Quantity * Price) + Commission)
ACBTotal = ACBTotal + ((Quantity * Price) + Commission)
QtyTotal = QtyTotal + Quantity
Case "Sell"
ACBChange = -ACBTotal / QtyTotal * Quantity
ACBTotal = ACBTotal / QtyTotal * (QtyTotal - Quantity)
QtyTotal = QtyTotal - Quantity
Case Else
MsgBox "Buy/Sell not specified"
End Select
fACBChange = ACBChange
End Function
Function fTtlHolding(Security As String, BS As String, Quantity As Long) As Long
Static QtyTotal As Long
If Security = oldSecurity Then 'reset for next security
ACBTotal = 0
QtyTotal = 0
oldSecurity = Security
End If
Select Case BS
Case "Buy"
QtyTotal = QtyTotal + Quantity
Case "Sell"
QtyTotal = QtyTotal - Quantity
Case Else
MsgBox "Buy/Sell not specified"
End Select
fTtlHolding = QtyTotal
End Function