Author |
Message |
Goldberg
Groupie
Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
|
Topic: Avoiding error when getting age by date of birth Posted: 21 Jun 2011 at 5:44am |
Hello,
I have the following code which retrieves the age of a person by date differing the current date and the date of birth. The piece of code I have is:
DateVar DNF:= Date({Alunos.DataNascFo}); DateVar Ann := CurrentDate;
If (Month(Ann) * 100) + Day (Ann) >=(Month(DNF) *100) + Day (DNF) then Year (Ann) - Year(DNF) Else Year (Ann) - Year(DNF) -1
Thing is, I get an error when there is no date of birth inserted in the DataNascFo field because it can't convert "" into a date, so, before the code above, I tried to do:
if {Alunos.DataNascFo} <> "" then
DateVar DNF:= Date({Alunos.DataNascFo});
DateVar Ann := CurrentDate;
If (Month(Ann) * 100) + Day (Ann) >=(Month(DNF) *100) + Day (DNF) then
Year (Ann) - Year(DNF)
Else
Year (Ann) - Year(DNF) -1
But when it shows a person with no date of birth inserted, it just places the age of birth of the previous person. At the end of the code I tried to do something like "else "" ", but it shows an error.
What can I do to avoid this problem? Thanks
|
IP Logged |
|
kostya1122
Senior Member
Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
|
Posted: 21 Jun 2011 at 5:52am |
you could try using a different formula to retrieve age like (currentdate - Date({Alunos.DataNascFo}))/365.5
|
IP Logged |
|
Goldberg
Groupie
Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
|
Posted: 21 Jun 2011 at 5:59am |
That code will have the same problem as the one I'm using. It will show an error if there is no date of birth. The code I'm using is working well, by the way, it is just not checking if the date of birth is ""
|
IP Logged |
|
kostya1122
Senior Member
Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
|
Posted: 21 Jun 2011 at 6:03am |
have you tried? logical it would show (currentdate)/365.5
|
IP Logged |
|
Goldberg
Groupie
Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
|
Posted: 21 Jun 2011 at 6:23am |
It does work, but, like I said, it will show an error if the date of birth is empty.
|
IP Logged |
|
kostya1122
Senior Member
Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
|
Posted: 21 Jun 2011 at 6:27am |
you could replace dateofbirth field with a formula like if isnull(dateofbirth field) then 1 or somthing
|
IP Logged |
|
Goldberg
Groupie
Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
|
Posted: 21 Jun 2011 at 9:59pm |
I did at the beginning "if {Alunos.DataNascFo} <> "" then" but but "when it shows a person with no date of birth inserted, it just places the age of birth of the previous person"
|
IP Logged |
|
yggdrasil
Senior Member
Joined: 19 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 150
|
Posted: 22 Jun 2011 at 2:53am |
You do need an ELSE to tell it what to do if the date of birth is null.
But you can't set it to "" because it has to be a number.
You could try setting it to -1, for the purposes of the calculation, then turning that back into a blank string for the display
|
IP Logged |
|
sharona
Senior Member
Joined: 16 Oct 2008
Location: United States
Online Status: Offline
Posts: 255
|
Posted: 22 Jun 2011 at 3:14am |
go to file
report options
check off convert null value boxes
this should pull them in as a zero.
if {Alunos.DataNascFo} =0 then 0 else
DateVar DNF:= Date({Alunos.DataNascFo}); DateVar Ann := CurrentDate;
If (Month(Ann) * 100) + Day (Ann) >=(Month(DNF) *100) + Day (DNF) then Year (Ann) - Year(DNF) Else Year (Ann) - Year(DNF) -1
|
sharona
|
IP Logged |
|
Goldberg
Groupie
Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
|
Posted: 24 Jun 2011 at 4:52am |
Thanks, I guess it is ok now.
|
IP Logged |
|
|