Results 1 to 4 of 4
  1. #1
    Plender is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2017
    Posts
    2

    Running Total Query

    I am relatively new to Access and, for context, I am currently building a database to track my investment portfolio. One thing that I am looking to track is the adjusted cost base for each security for income tax purposes. The problem I am currently having is that the adjusted cost base is a running total and is dependent on previous "Buy" and "Sell" transactions and the sequence they occurred in.

    The table for my transaction data looks something like this:

    TransactionID Security TransactionType TransactionDate Quantity Price Commission
    1 ABC Buy 01-Jan-17 400 $20.00 $50.00
    2 ABC Buy 02-Jan-17 100 $19.00 $50.00
    3 ABC Buy 03-Jan-17 100 $21.00 $50.00
    4 ABC Sell 04-Jan-17 300 $22.50 $50.00
    5 ABC Buy 05-Jan-17 100 $22.00 $50.00
    6 ABC Sell 06-Jan-17 300 $23.00 $50.00

    I'm looking for a query that will generate something similar to the following:

    ACBDate TotalHoldings ACBChange ACB
    01-Jan-17 400 $8,050.00 $8,050.00
    02-Jan-17 500 $1,950.00 $10,000.00
    03-Jan-17 600 $2,150.00 $12,150.00
    04-Jan-17 300 ($6,075.00) $6,075.00
    05-Jan-17 400 $2,250.00 $8,325.00
    06-Jan-17 100 ($6,243.75) $2,081.25

    For reference:


    • TotalHoldings is a running total of Quantity bought less Quantity sold
    • ACBChange
      • For a "Buy" transaction is: Quantity * Price + Commission
      • For a "Sell" transaction is: -1 * ((Prior Period ACB) / (Prior Period TotalHoldings) * Quantity)

    • ACB is a running total of ACBChange


    I've tried using the DSUM function and I have something that seems to be working, but takes >10 seconds to calculate despite having only approximately 200 transactions. I feel like this is something I could build in Excel which would take less than a second to calculate, so I assume there's a better way. Does anyone have a suggestion on how you might approach this differently? I have also tried using subqueries, but I am getting a circular reference error because the ACB when it is a "Sell" transaction is dependent on the Total ACB in the prior period which is dependent on if there were previous "Sell" transactions.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    the nature of what you are trying to do is recursive because of the nature of the sell calculation (which incidentally shouldn't you be adjusting for the sell commission?)

    Access sql doesn't have recursive capabilities although it can be mimicked with multiple queries if you know how many levels you need to go down - and my guess this would not be the case.

    If your backend is sql server, it can be done because tsql has recursive capabilities. In Access you can also do it by creating a vba function - or perhaps a normal one.

    If you want to go the vba route the function would need to

    have passed a parameters the security and trandate
    would open a recordset something like "SELECT * FROM tranTable WHERE security= '" & security & "' AND trandate<" & trandate
    then step through each record and calculate and accrue the relevant figures

    However I don't have a quick fix for you other than this may be one of those rare situations where you need to store a calculated value at the time the record is entered. Or to pass the data to excel and do it there

  3. #3
    Plender is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2017
    Posts
    2
    Thanks Ajax, I was starting to get the feeling that might be the case.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    this probably needs more work, but this works on the example data you provided

    Click image for larger version. 

Name:	Capture.JPG 
Views:	13 
Size:	33.0 KB 
ID:	29267


    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
    Last edited by CJ_London; 06-29-2017 at 02:24 AM.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Running Total within a Union Query
    By barkly in forum Queries
    Replies: 7
    Last Post: 06-25-2017, 08:30 PM
  2. Help With Running Total Query
    By Njliven in forum Queries
    Replies: 27
    Last Post: 08-06-2014, 10:27 AM
  3. Running Total Query??
    By kwooten in forum Queries
    Replies: 8
    Last Post: 06-15-2012, 06:10 AM
  4. running or sub total using sql query in access
    By learning_graccess in forum Access
    Replies: 4
    Last Post: 10-15-2011, 05:40 AM
  5. Creating a Daily Running Total Query
    By seraph in forum Queries
    Replies: 0
    Last Post: 08-15-2009, 12:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums