Results 1 to 9 of 9
  1. #1
    Takiman is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2017
    Posts
    21

    Calculate the value difference between two consecutive records in my subform

    Hi how do I calculate the difference between two consecutive records?
    Both consecutive record values are shown in my subform (continuous form view) linked to my mainform by AuditID. I have also limited the subform to only allow two records to be created per mainform record. This is as per my requirements, so I would only need to find the difference between two values per Mainform record.

    Below shows the source table for my subform. Therefore, based on my requirements I would need my first calculation to be: $50,000-$20,000 =$30,000. And therefore, my second calculation would be $15,000-$35,000 = -$20,000.. and so on.



    Any help with trying to work this out would be great. Thanks.

    tblAuditCost
    AuditCostID AuditID CostID CostValue
    1 1 1 $20,000
    2 1 4 $50,000
    3 2 9 $35,000
    4 2 7 $15,000

  2. #2
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Grouped query using audit id? And difference between max and min?


    Sent from my iPhone using Tapatalk

  3. #3
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Or dmax - dmin functions where auditid = 2


    Sent from my iPhone using Tapatalk

  4. #4
    Takiman is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2017
    Posts
    21
    Quote Originally Posted by andy49 View Post
    Or dmax - dmin functions where auditid = 2
    Sent from my iPhone using Tapatalk
    thanks for advice andy. I am new to programming in access so any help with scripts would be appreciated. thanks.

  5. #5
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    If you had this in a textbox then maybe

    Dmax("yourfield","yourtable", "where auditid = 1") -dmin( "yourfield","yourtable","where auditId = 1")

    might get you started. Yourfield is costValue and table is tblauditcost.


    Sent from my iPhone using Tapatalk

  6. #6
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Based on your two examples, it appears that you want to subtract the first record value FROM the second, so using the min/max of the AuditID field won't give you the order I think you're after. If you include the AuditCostID in the subform records (assuming this is a subform datasheet) then either DLookup or DMax in a calculated control might be possible. I think the solution depends on what the subform is based on. If that is a table, then there's more records to deal with than if it is a query that only returns two records. If it's a table, a function would be one way to filter the records as well as place them in the correct order for subtraction. This would be far easier if you only wanted the difference and didn't care about the order. I'm also assuming what you have is a subform datasheet.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    If order matters whatabout group query then use first and last rather than min and max?


    Sent from my iPhone using Tapatalk

  8. #8
    Takiman is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2017
    Posts
    21
    Ok thanks Andy and Micron for advice I will give it go tmw eve and let you know if i can get it to work -- I had some pc h/w problems today so have been offline :-(
    and Micron yes my Subform feeds into a table (tblAuditCost) so there would be more than two records as each Audit will creates two records which are added to the subform table. The PK of that subform table is AuditCostID, however, the AuditID number will be the same for each of the two records created, so was thinking I would just need to work out the difference between AuditID's that are equal in value, and as you have aptly suggest subtract the first record value FROM the second (so the logic would be (based on my example): AuditCostID 2 (CostID 4 CostValue) subtract AuditIDCost 1 (CostID 1 CostValue), for AuditID 1. Again I will have a look tmw eve ,thanks for tips so far

    tblAuditCost
    AuditCostID AuditID CostID CostValue
    1 1 1 $20,000
    2 1 4 $50,000
    3 2 9 $35,000
    4 2 7 $15,000

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Allen Browne gives an example of doing just this, using a Subquery, here

    http://allenbrowne.com/subquery-01.html

    Go down to the heading

    Get the value in another record

    I suppose you may be able to base the Subform on such a Subquery to accomplish your goal.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Date Difference between consecutive rows
    By shod90 in forum Forms
    Replies: 4
    Last Post: 03-07-2017, 09:05 AM
  2. Calculate difference from last value
    By dilbert in forum Reports
    Replies: 4
    Last Post: 01-17-2016, 08:06 AM
  3. Calculate time difference
    By desireemm1 in forum Access
    Replies: 19
    Last Post: 10-27-2014, 12:06 PM
  4. Replies: 7
    Last Post: 07-02-2014, 09:30 AM
  5. Replies: 2
    Last Post: 01-23-2013, 11:07 AM

Tags for this Thread

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