Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2025
    Location
    Idaho
    Posts
    1

    Find diff of consequtive values in subform

    I have a database I built several years ago that I've used to record printer usage tallies (monthly). In a subform listing the usage tallies for individual printers, I'd like the subform to show the difference between the tally before and the current tally. For example, PrinterA had a tally of 2146 pages (in it's lifetime) on 12/1/2020 and on 1/1/2021 a tally of 2649. What is the difference between the tallies? The difference between 1/1/2021 and 12/1/2020 would be 503. How can I calculate that in the subform?

    Example pic of what the form and subform look like:


    Click image for larger version. 

Name:	PagesCountsFormView.jpg 
Views:	19 
Size:	81.6 KB 
ID:	52578


    I'd like to have columns show the difference for the PrinterPages, CopierPages, MonoPages, ColorPages, ScanImages, LedgerPages, LedgerScans ... but I haven't managed to figure out how to do that.

    I'm a printer tech in charge of managing over 500 printers and these tallies are invaluable for managing and planning replacements. In the past, I've been copying the data from the database, pasting it into an Excel sheet that has additional columns doing the calculation (easy to do in Excel... apparently not so simple in an Access subform). I'd like the database to show me that calculation without having to manually copy/paste.

    Can you help?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Pulling data from another record of same table is not simple. Options:

    Correlated subquery http://allenbrowne.com/subquery-01.html#AnotherRecord
    You want the prior value for 7? fields, that means 7 nested queries.

    Self-join query - gets kind of messy because of date calculation and gets messier if there are gaps in date.

    Domain aggregate function expression in textbox.
    Unfortunately, calculating first day of previous month is not simple but the month and year parts are really all that is needed. So calculate last day of previous month and extract yearmonth (again, no date gaps - if there are this gets more complicated).
    =DLookup("PrinterPages","MyTable","PrinterID=" & [PrinterID & " AND Format([Date],'yyyymm')=" & Format(DateSerial(Year([Date]),Month([Date]),0),"yyyymm"))


    The two query options seem to result in a non-editable dataset - not good for a data entry form. Do you want this form to be editable?


    Really should not use reserved words as names. Date is a reserved word because it is an intrinsic function.
    Last edited by June7; 01-17-2025 at 07:56 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    If there is only one reading per month as your data implies, you can get a conversion comparison on a format of yyyymm for your self join.

    Basic query might be something like this

    Code:
    SELECT *
    FROM 
        (SELECT *, Format([readDate],"yymm") AS Curr) AS Prev FROM Printers)  AS latest
    INNER JOIN 
        (SELECT *, Format(DateAdd("m",-1,[readDate]),"yymm") AS Prev FROM Printers) AS Previous
     ON (latest.Curr = Previous.Prev) AND (latest.printerFK = Previous.printerFK)
    WHERE latest.printerFK=1200
    Using calculated values like this in your joins can be slow due to the fact they cannot be indexed

    edit: just realised you want a year difference - so use -12 rather than -1 in the second aliased query (Previous)

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    CJ, difference between 1/1/2021 and 12/1/2020 is not a year, OP is asking for monthly change but the annual option good to know. Your SQL is pretty much what I had in mind for second option but would not include static WHERE clause.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I've been copying the data from the database, pasting it into an Excel sheet that has additional columns doing the calculation
    Might be easier to stay with Excel since you already have that set up, but don't copy/paste - link to the Access table from Excel?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 3
    Last Post: 06-29-2012, 01:33 AM
  2. Replies: 1
    Last Post: 07-08-2011, 03:41 AM
  3. Button to run Update Query in Diff Database
    By Juan4412 in forum Queries
    Replies: 8
    Last Post: 06-24-2011, 05:45 AM
  4. Date diff trouble
    By Cran29 in forum Queries
    Replies: 3
    Last Post: 03-11-2011, 01:02 PM
  5. Check time Diff.
    By wes028 in forum Access
    Replies: 3
    Last Post: 03-08-2010, 10:05 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