Results 1 to 7 of 7
  1. #1
    djclntn is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    476

    How to count back # of days to current dat

    I would like to have a field which how many days passed from the date imputted to the current date.


    Music/Artists Form


    DatePlayed: 11/3/2015
    *CurrentDate: 11/5/2015
    Days Last Played (DLP): 2


    Expression I tried: DLP: Format [(TDate]-Date())


    *not a field

  2. #2
    bigot is offline Advanced...ish
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    37
    So DatePlayed is a field (what's the type?)

    I'm not sure if it's a transposition error, but your opening brackets are in the wrong order
    Code:
    Format [(...
    instead of
    Code:
    Format ([...

  3. #3
    djclntn is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    476
    Thanks for responding to my post. It's a Date/Time Field & yes I'm getting an "opening bracket are in the wrong order" error

  4. #4
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Replace
    Code:
    DLP: Format [(TDate]-Date())
    with
    Code:
    DLP: [TDate]-Date()

  5. #5
    bigot is offline Advanced...ish
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    37
    Ok perfect, since the Date() function also returns Date/Time, you can just subtract the two.

    Now you're using the "Format" function but omitting the second parameter, (which programmatically works, since that parameter is optional) but I think it defaults to "dd/mm/yyyyy" or "mm/dd/yyyy" (sorry, I'm not sure which, regardless...) you want to retrieve the number of days, not a date.

    If you're thinking of using Format( ..., "d" ), think again! If the difference is more than 31 days, it will screw up! (I can explain on request, but it should be easy to figure out)

    I suggest you do
    Code:
    CLng( Date() - TDate )
    See "CLng" converts the difference to a Long. Which will give you the number of days.

    Extra info: Now be careful not to use CInt, because Integers can only handle up to a value of 32,767. The current date (as of writing this) is 42,313. So, the difference between Date() and a TDate of 0 would overflow an Integer.

    PS. InsuranceGuy's code above is incorrect (though I think he was just fixing your brackets without reading). You want to subtract TDate from Date ( Date() - TDate ), not the other way around ( TDate - Date() ). Think about it: If they listened to the song yesterday, subtracting today's date would give you -1, not +1!

  6. #6
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126

    Talking

    Quote Originally Posted by bigot View Post

    PS. InsuranceGuy's code above is incorrect (though I think he was just fixing your brackets without reading). You want to subtract TDate from Date ( Date() - TDate ), not the other way around ( TDate - Date() ). Think about it: If they listened to the song yesterday, subtracting today's date would give you -1, not +1!
    Interesting take! I was just letting him keep his negative. I use negatives vs. positive day counts intentionally. )

  7. #7
    bigot is offline Advanced...ish
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2014
    Posts
    37
    Quote Originally Posted by InsuranceGuy View Post
    Interesting take! I was just letting him keep his negative. I use negatives vs. positive day counts intentionally. )
    That can be useful for certain types of date comparisons. Since DatePlayed will always be in the past and Date() generates today's date, your formula would never generate a positive

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

Similar Threads

  1. Replies: 1
    Last Post: 05-22-2014, 06:32 AM
  2. Count Days...
    By sdc1234 in forum Queries
    Replies: 1
    Last Post: 02-06-2014, 09:31 AM
  3. Count how many days since and on what date
    By burrina in forum Queries
    Replies: 28
    Last Post: 01-26-2013, 11:57 AM
  4. Count number of days
    By ramindya in forum Queries
    Replies: 1
    Last Post: 02-22-2012, 04:47 PM
  5. Replies: 4
    Last Post: 11-10-2011, 03:16 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