Results 1 to 10 of 10
  1. #1
    Cdray is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5

    Help with a calculation

    I have a spot in my table where dates are tied with numbers i.e.



    Datefield: 31 Apr 15
    Numberfield: 56000

    Every month for each client the month end date is added and a number put it. So each date field and number field is of course tied to a ClientID basically (the master key).

    This is all fine, but what I'm trying to do is get a table value calculation which is a subtraction of the month end date within the last 31 days, from the month end date in the last 62 days (basically the most recent month from the most recent before that).

    Datefield: 31 Apr 15
    Numberfield: 56000

    Datefield: 31 May 15
    Numberfield: 58000

    I want it to subtract 56000 from 58000.

    I know how to get dates, though it may not be the best way "(Between Date() And DateAdd("m",-1,Date())". But how do I tie that to the numberfield and subtract them?

    This field I'm trying to add, it should auto populate as 2,000, based on the calculation.

    Any ideas?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    So you need to pull data from another record of the same table? One method: http://allenbrowne.com/subquery-01.html#AnotherRecord
    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.

  3. #3
    Cdray is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    I'm not sure how to use this? I don't understand how to correlate that with mine. All the data I have is CustomerID (Primary key), Datefield, and numberfield.

    For each customer they have infinite datefields with 1 numberfield per date field, and I need to subtract the most recent numberfield from the 2nd most recent.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    There is no 31 April!!!!!!!!!!!!!!!!!!
    Do you have a clear description in plain English of what you are trying to do?

    Access had Date-related functions and can handle dates and differences in dates quite well.

  5. #5
    Cdray is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    Hi, sorry for the confusion.

    Today's June 25.

    So for a client I have

    Datefield: 31 May 15
    Numberfield: 56000

    These are on the same table, and the same record.

    Then I have

    Datefield: 30 Apr 15
    Numberfield: 54000


    I need it to automatically subtract the most recent number (56,000, tied to 31 May 15), from the number tied to the earlier date (54,000, tied to 30 Apr 15).

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Nested subquery is the way to pull previous record data to calc with in current record. Something like:

    SELECT ClientID, Datefield, Numberfield, (SELECT TOP 1 Dupe.Numberfield FROM tablename AS Dupe WHERE Dupe.ClientID = tablename.ClientID AND Dupe.Datefield < tablename.Datefield ORDER BY Dupe.Datefield DESC, ClientID) AS PriorValue FROM tablename;

    Now use that query as RecordSource for a report. Expression in textbox: =[Numberfield] - [PriorValue]
    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.

  7. #7
    Cdray is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    Quote Originally Posted by June7 View Post
    SELECT ClientID, Datefield, Numberfield, (SELECT TOP 1 Dupe.Numberfield FROM tablename AS Dupe WHERE Dupe.ClientID = tablename.ClientID AND Dupe.Datefield < tablename.Datefield ORDER BY Dupe.Datefield DESC, ClientID) AS PriorValue FROM tablename;

    Now use that query as RecordSource for a report. Expression in textbox: =[Numberfield] - [PriorValue]
    Where would I place that first line? I've designed a whole database but in a very rudimentary way as my programming is definitely not great.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    That is a complete SQL statement to build a query object. Open query designer, switch to SQL View, copy/paste the statement, change table and field names as appropriate.
    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.

  9. #9
    Cdray is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    I got a syntax error in the part with parenthesis. Not sure which is wrong, my table name has spaces in, does it need to underscore?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Advise not to use spaces and special characters/punctuation (underscore is exception) in naming convention. If used, enclose name in [].

    FROM [table name]
    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.

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

Similar Threads

  1. How to do this calculation
    By rickroller5 in forum Access
    Replies: 4
    Last Post: 09-07-2014, 01:22 PM
  2. Calculation Field in table vs Query calculation
    By Leonidsg in forum Database Design
    Replies: 18
    Last Post: 02-28-2013, 09:38 PM
  3. Pay calculation
    By shankar.nst in forum Programming
    Replies: 2
    Last Post: 09-29-2011, 12:15 PM
  4. Calculation value not less than 1
    By vbpeterson in forum Queries
    Replies: 10
    Last Post: 08-17-2011, 06:41 AM
  5. How to do calculation???
    By latestgood in forum Forms
    Replies: 0
    Last Post: 05-18-2011, 11: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