Results 1 to 9 of 9
  1. #1
    Girraffa is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    32

    Unhappy Trying to write a function but it keeps returning "0" no matter the input

    Hello folks,
    I am trying to learn how to write functions and have been working on a very simple one that should return the age of an animal in years. I have written one but it keeps returning zero when it should return years with decimal points, I am having trouble understanding what I am doing wrong and I am hoping someone can correct me.
    Code:
    Public Function AgeAtDeath( _
                dtBirth As Date, _
                dtDeath As Date) As Double
                Dim CalcAge As Double
                CalcAge = DateDiff("d", dtBirth, dtDeath)
                AgeAtDeath = Round(CalcAge, 1)
    End Function
    This would be used in a query for a form or report. I was trying this with the immediate window feeding it random dates as follows:
    ?AgeAtDeath(2/13/2010, 2/15/2018)


    but all it gives me is zero. Thank you for your help!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You need delimiters:

    ?AgeAtDeath(#2/13/2010#, #2/15/2018#)
    2924
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To elaborate, without delimiters, it assumes it's a number, so 2 divided by 13 divided by 2010
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    I am trying to learn how to write functions and have been working on a very simple one that should return the age of an animal in years.
    Code:
    CalcAge = DateDiff("d", dtBirth, dtDeath)
    Which do you want - age in years or days?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Girraffa is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    32
    Quote Originally Posted by pbaldy View Post
    You need delimiters:

    ?AgeAtDeath(#2/13/2010#, #2/15/2018#)
    2924
    Oh my goodness I forgot about those, thank you so much!!!!!

  6. #6
    Girraffa is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    32
    Quote Originally Posted by ridders52 View Post
    Code:
    CalcAge = DateDiff("d", dtBirth, dtDeath)
    Which do you want - age in years or days?
    You're right, I want it in years and forgot to add the division part. so this is really what I should have aye? Thank you!!!

    Code:
    Public Function AgeAtDeath( _
                dtBirth As Date, _
                dtDeath As Date) As Double
                Dim CalcAge As Double
                CalcAge = DateDiff("d", dtBirth, dtDeath)
                AgeAtDeath = Round(CalcAge / 365.25, 1)
    End Function

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Not that it's real important, but you don't really need the Double variable CalcAge...

    Code:
    Public Function AgeAtDeath(dtBirth As Date, dtDeath As Date) As Double
       AgeAtDeath = Round(DateDiff("d", dtBirth, dtDeath), 1)
    End Function
    Just trying to save the world from using too many characters. Pretty soon we'll run out.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Girraffa View Post
    Oh my goodness I forgot about those, thank you so much!!!!!
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Girraffa is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    32
    Quote Originally Posted by Micron View Post
    Not that it's real important, but you don't really need the Double variable CalcAge...

    Code:
    Public Function AgeAtDeath(dtBirth As Date, dtDeath As Date) As Double
       AgeAtDeath = Round(DateDiff("d", dtBirth, dtDeath), 1)
    End Function
    Just trying to save the world from using too many characters. Pretty soon we'll run out.
    Thanks! I was wondering about that, it seemed like an unnecessary step.

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

Similar Threads

  1. Suppress "Requery" and/or "Repaint" when running function
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 12-08-2017, 12:04 AM
  2. Replies: 6
    Last Post: 06-24-2016, 06:07 AM
  3. Replies: 3
    Last Post: 04-20-2016, 02:50 PM
  4. Replies: 1
    Last Post: 07-30-2015, 05:03 AM
  5. Replies: 5
    Last Post: 08-10-2010, 02:57 PM

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