Results 1 to 8 of 8
  1. #1
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31

    Formula returns with #Error everytime Access is opened

    Dear Experts,

    I have create a a form from a Query. In query design view i have entered a formula to calculate Age, Passport expiry days remaining and other few similar formulas. However, when i close the and open the access, the cell in form automatically changes to #Error.

    Procedures i have to follow to get it back.

    1. Go to Query Design view
    2. Click on the Age Field
    3. Click Zoom and then click OK inside zoom

    Formulas i used in my DB

    AGE: DateDiff("yyyy",[DATE_OF_BIRTH],Date())-IIf(Format([DATE_OF_BIRTH],"mmdd")>Format(Date(),"mmdd"),1,0)




    PPT_EXP DAYS: DateDiff("d",Date(),[PP EXPIRY])


    QID_Exp_Remining_Days: DateDiff("d",Date(),[VISA/QID_EXPIRY])


    HC_COUNTDOWN: DateDiff("d",Date(),[HC_EXPIRY])



    Once above is done, then the #Error changes to a number as value (i.e. Example Age 35). I am not sure why this happens everytime.
    Below is the screenshots for your reference.

    Please suggest me if i am doing something wrong in my formula.

    Kind regards,
    AB
    Attached Thumbnails Attached Thumbnails Query Error 1.JPG   Design view formula.JPG  
    Last edited by aliaslamy2k; 05-12-2020 at 11:24 AM. Reason: Updated Formulas in question

  2. #2
    Cottonshirt is offline Competent Performer
    Windows 8 Access 2007
    Join Date
    Feb 2018
    Posts
    116
    I don't think you need to Format() the two dates in the IIF() statement. normally, Format() is only used to prepare a date for viewing by a person, if no one will ever see it then formatting a date serves no purpose at all.

    in your expression, you have a year in the first part from which you are subtracting a date in mmdd format, which I suspect confuses ACCESS almost as much as it confuses me.

    try this: IIF([dateofbirth]>Date(),1,0) which works on my system.


    good luck with your project,


    Cottonshirt

  3. #3
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31
    Hi CottonShirt,

    I tried your formula, but it returns the value as zero. Please see attached screenshots.
    Attached Thumbnails Attached Thumbnails DOB.JPG   DOB DATAVIEW.JPG  

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I agree that usually should not use Format() function when calculating with full date value (unless you have to deal with international dates http://allenbrowne.com/ser-36.html). Format() returns a string value.

    You need to use suggested expression instead of the IIf() in yours.

    AGE: DateDiff("yyyy",[DATE_OF_BIRTH],Date()) - IIf([dateofbirth]>Date(),1,0)

    However, would a date of birth ever be greater than current date?
    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.

  5. #5
    aliaslamy2k is offline Advanced Beginner
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2020
    Posts
    31
    Hello June7,

    Thank you so much, Its working now.

    I am new to access formulas and trying hard to learn as much as i could with the help of this Forum.

    Answer to your question is : DOB cannot be greater than current date. Apologize for my misunderstanding.

    The formula is working perfectly but i still don't understand how this formula works. Why we have to use " - IIf([dateofbirth]>Date(),1,0)" and what does 1,0 stands for.

    Kind regards,
    AB

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    IIf() function syntax in simple English is:

    IIf(expression, return this value if expression true, else return this value)

    1 and 0 are just values.
    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
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    Why we have to use " - IIf([dateofbirth]>Date(),1,0)" and what does 1,0 stands for.
    you don't - the original formula was this

    -IIf(Format([DATE_OF_BIRTH],"mmdd")>Format(Date(),"mmdd"),1,0)

    it is basically subtracting 1 from the age by looking at the months and days of the DOB v date

    a simpler and more efficient formula would be

    +(
    Format([DATE_OF_BIRTH],"mmdd")>Format(Date(),"mmdd"))


    So say DOB is 1st March 2000 and today is 12th May 2020

    your formula

    DateDiff("yyyy",[DATE_OF_BIRTH],Date()) returns 20 which is correct

    but if the DOB is 1st June 2000, it also returns 20 - but that birthday hasn't happened yet - they are still 19

    so you need to deduct 1 if the birthday has not yet occurred this year - hence the formula

    +(Format([DATE_OF_BIRTH],"mmdd")>Format(Date(),"mmdd"))

    this returns a boolean value of either true or false - true is represented by -1, false is represented by 0

    so the whole formula

    DateDiff("yyyy",[DATE_OF_BIRTH],Date())+(Format([DATE_OF_BIRTH],"mmdd")>Format(Date(),"mmdd"))

    DOB=1st March................20..................+........ .............................0 = 20
    DOM=1st June.................20..................+
    .....................................-1 = 19



  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    I don't think the new formula would give you the same results as your original one. I think in your original one you were trying to subtract one year from the number of years returned by DateDiff if the dateof birth for the current year falls in a month following the current one. And that will never happen (as you noticed in post #3) with IIf([dateofbirth]>Date(),1,0) as it will always return 0. You can use the DateSerial function(https://docs.microsoft.com/en-us/off...erial-function) to build this year's birthdate and compare that to Date():

    IIf(DateSerial(Year(Date()),Month([DATE_OF_BIRTH]),Day([DATE_OF_BIRTH])) >Date(),1,0)

    Or you can try to use your original formula but put it straight into the control sources of the textboxes that are giving you trouble.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Run-time error 91, but not everytime the code runs
    By mcomp72 in forum Programming
    Replies: 30
    Last Post: 11-24-2017, 10:44 PM
  2. Replies: 14
    Last Post: 08-23-2014, 12:35 PM
  3. Replies: 4
    Last Post: 05-08-2013, 11:56 AM
  4. Access crashes everytime I run applyfilter??
    By latestgood in forum Access
    Replies: 1
    Last Post: 06-22-2011, 12:25 PM
  5. Field returns error when I import Text Files to Access
    By geng in forum Import/Export Data
    Replies: 3
    Last Post: 06-01-2010, 02:20 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