Results 1 to 8 of 8
  1. #1
    lathander is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    5

    +/- calculations

    Hi, I'm not sure where the correct subboard for this question would be, but I'm trying to add a calculation for an inventory tracking database and I'm wondering if this sort of thing is possible with the access functions.

    So my fields are as follows:

    Date
    Item Name
    QTY1
    In/Out
    QTY2


    QTY3

    And o I want QTY 3 to calculate QTY 1 + QTY 2 if the 4th field is "in", but have it calculate QTY 1 - QTY 2 if the 4th field is "out".

    Thanks in advance.

  2. #2
    Join Date
    Apr 2017
    Posts
    1,679
    You need tables like
    tblStorage: StorageID, EntryDate, EntryType, ItemID, Qty;
    tblEntryTypes: EntryType, Description, Direction (where direction has values 1 for entry types marking incoming, and -1 for entry types marking outgoing movements);
    tblItems: ItemID, ItemName, ...;

    To calculate the quantity of item "MyItem" in stock at date "MyDate":
    Code:
    SELECT  it.MyItem, SUM(et.Direction*st.Qty) AS InStock
    FROM (tblStorage st LEFT JOIN tblEntryTypes et ON et.EntryType = st.EntryType) LEFT JOIN tblItems it ON it.ItemID = st.ItemID
    WHERE EntryDate <= MyDate
    GROUP BY it.MyItem
    NB! You have to get the syntax for date limit in WHERE clause right!

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    you can have something like this in a query

    QTY3:QTY1 + iif([in/out]="in",QTY2,-QTY2)

  4. #4
    lathander is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    5
    Thanks for the answers. I will test both out.

    A follow-up question, though, can this be linked to a form?

  5. #5
    Join Date
    Apr 2017
    Posts
    1,679
    Quote Originally Posted by lathander View Post
    A follow-up question, though, can this be linked to a form?
    With my solution, probably the easiest way is to write an UDF, which constructs a similar querystring for active row in form, ant then runs it and returns the result value.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784
    I don't know why no one commented on your un-normalized design. What if you need another QTY, you add a field? Also, you are storing a calculated result. The qty of a product should be 1 when added, -1 when removed. qty on hand is the sum of all +'s and -'s for a product.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    lathander is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    5
    Hm, I just realized that there might be simultaneous incoming/outgoing in one day, so perhaps it's better set the fields like follows:

    - QTY on hand (1)
    - Incoming QTY (2)
    - Outgoing QTY (3)
    - Final QTY : (1) + (2) - (3)

    I don't want the final QTY to be on the form though.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    as has been suggested by avril and micron, if this is supposed to calculate a closing balance it should be calculated as and when required

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

Similar Threads

  1. Calculations
    By Burnsie in forum Forms
    Replies: 7
    Last Post: 02-22-2018, 06:15 PM
  2. Calculations
    By carlislewwtp in forum Access
    Replies: 3
    Last Post: 01-29-2018, 02:32 PM
  3. Replies: 30
    Last Post: 09-30-2015, 10:58 AM
  4. calculations
    By frustratedwithaccess in forum Queries
    Replies: 5
    Last Post: 01-08-2015, 01:56 PM
  5. calculations
    By DariusD in forum Access
    Replies: 3
    Last Post: 01-01-2013, 07:22 PM

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