Error trying to calculate age - InfoPath Dev
in

InfoPath Dev

Error trying to calculate age

Last post 02-09-2009 01:21 AM by Ian Dawson. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 02-04-2009 10:01 PM

    • Ian Dawson
    • Top 50 Contributor
      Male
    • Joined on 01-01-2009
    • Shellharbour NSW Australia
    • Posts 154

    Error trying to calculate age

    Hi,

    I am trying to calculate the  age of a person, the field name is cust_dob

    the formulae I used,     I copied and modified from posts on calculating age, 
                       in an expression box is

    floor(  (   number(substring(today(), 1, 4))
                 + number(substring(today(), 6, 2)) / 12
                 + number(substring(today(), 9, 2)) / 365.25)
              - (  number(substring(@cust_dob, 1, 4))
                 + number(substring(@cust_dob, 6, 2)) / 12
                 + number(substring(@cust_dob, 9, 2)) / 365.25))

    I kept getting an error at the first @ symbol, when trying to verify the formula  
    Why???

    I stripped down the equation to just
                         number(substring(cust_dob(), 1, 4))
    and was getting
    a "NaN"   appearing on the expression box.

    I continued stripping the formula down to just substring(@cust_dob, 1, 4).
    And I actually got a correct value.

    I have gone back and started from scratch and copied the formula using all the Fx keys as is available, I found I was getting Node errors until I put a space before the 12 and the 365.25.
    This time I was able to successfully create the formula without any Verify errors.  BUT NaN appeared!!!!!!
    What am I doing wrong?? 
    In the meantime I shall continue to try to resolve.

    Any Clues as to why anyone to a successful outcome??

  • 02-04-2009 10:39 PM In reply to

    • Anuma
    • Top 25 Contributor
      Female
    • Joined on 07-20-2005
    • India
    • Posts 525

    Re: Error trying to calculate age

  • 02-04-2009 10:46 PM In reply to

    • Ian Dawson
    • Top 50 Contributor
      Male
    • Joined on 01-01-2009
    • Shellharbour NSW Australia
    • Posts 154

    Re: Error trying to calculate age

    Hi Anuma,

    I think your method in the link is a bit complicated. Because you are trying to analyse the months individually, and the days also. It is not necessary.

    The best solution is to convert the whole date to a decimal value, deduct one date from the other and find the whole integer part of that number.

    As in the formula I was using. 

    I have updated my original post with a few more comments about my investigations.

    I have tested some individual parts on their own, and formula seems Okay.

    But I am still having the Nan problem?? 

    Any clues?

    No I found the solution which makes another problem.

    The solution as I see it is -- until you run the procedure with a record, the NaN will appear.
                                                  in a Textbox field  Bright Red.
                                                  in the Expression box Black.
    Then all seems okay. Except unless you enclose each date in brackets, the equation can end up wrong.
    Try using a date like 4/12/51 and today and see what results you get.
    I had the extra brackets in the expression box and got 57 in the text box, without the extra brackets, I got 59.

     

    The remaining problem now is how to avoid getting the Nan appearing?

    Thanks Anuma if you have an answer?

  • 02-05-2009 06:44 AM In reply to

    Re: Error trying to calculate age

    I have no idea what you mean when you say "extra brackets."  To what are you referring?

     If you don't want the NaN to appear, you can add conditional formatting to the expression box to hide it when @cust_dob is blank.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-06-2009 03:05 AM In reply to

    • Ian Dawson
    • Top 50 Contributor
      Male
    • Joined on 01-01-2009
    • Shellharbour NSW Australia
    • Posts 154

    Re: Error trying to calculate age

     

    Thanks for that Jimmy, I will.

    Extra brackets. means brackets to enclose the calculation for today's date, and brackets around the entire calculation for the other date.  It is remarkable that it did happen. I think that round somehow must affect the small values for month and day of the month.

    Yet the brackets as I described above ensured a correct value.

  • 02-06-2009 10:25 AM In reply to

    Re: Error trying to calculate age

    Probbably a bit late in responding to this but i gave up on the rules eventually and just went for ....

     

            public static string GetAgeBand(string DateOfBirth, out string age)
            {
                string ageBand = string.Empty;
                string calcAge = string.Empty;

                if (DateOfBirth != string.Empty)
                {
                    DateTime birthDate = Convert.ToDateTime(DateOfBirth);
                    // get the difference in years
                    int years = DateTime.Now.Year - birthDate.Year;
                    // subtract another year if we're before the birth day in the current year
                    if (DateTime.Now.Month < birthDate.Month ||
                    (DateTime.Now.Month == birthDate.Month &&
                    DateTime.Now.Day < birthDate.Day))
                        years--;
                    if (years < 18)
                        ageBand = "Under 18";
                    if (years >= 18 && years < 65)
                        ageBand = "18 - 64";
                    if (years >= 65)
                        ageBand = "65 And Over";
                    calcAge = years.ToString();
                }
                age = calcAge;
                return ageBand;
            }

            public void UserProfile_DateOfBirth_Changed(object sender, XmlEventArgs e)
            {
                string age = string.Empty;
                e.Source.SelectSingleNode("/my:Data/my:User/my:UserInformation/my:UserProfile/my:UserProfile_AgeBand", NameSpaceManager).SetValue(GetAgeBand(e.NewValue, out age));
                e.Source.SelectSingleNode("/my:Data/my:User/my:UserInformation/my:UserProfile/my:UserProfile_Age", NameSpaceManager).SetValue(age);
            }

     

    Obviously this returns an age band but you get the idea.

  • 02-09-2009 01:21 AM In reply to

    • Ian Dawson
    • Top 50 Contributor
      Male
    • Joined on 01-01-2009
    • Shellharbour NSW Australia
    • Posts 154

    Re: Error trying to calculate age

    I'll keep a copy of your code for future use. I am avoiding code as much as possible. 

    The code I wrote at the top is a default value box on the data window. I seemed to get it to work last week. 
    But today, the system seems to be screwing up the formula. I'll worry about it later. I have other problems.

    I had field issues with most of the other fields "control bound to missing field" or "Unbound, control canot store or data".
    Maybe because of those the calculation would not work. It is only a guess.

    But, I gave up on that form and building a new one. It seemed to be going ok, no errors in field construction.

    However, it has a Submit button that cannot submit, see my earlier thread.

Page 1 of 1 (7 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.