Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Avoiding error when getting age by date of birth Post Reply Post New Topic
Author Message
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet 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 IP Logged
kostya1122
Senior Member
Senior Member
Avatar

Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
Quote kostya1122 Replybullet 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 IP Logged
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet 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 IP Logged
kostya1122
Senior Member
Senior Member
Avatar

Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
Quote kostya1122 Replybullet Posted: 21 Jun 2011 at 6:03am
have you tried? logical it would show
(currentdate)/365.5


IP IP Logged
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet 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 IP Logged
kostya1122
Senior Member
Senior Member
Avatar

Joined: 13 Jun 2011
Online Status: Offline
Posts: 475
Quote kostya1122 Replybullet 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 IP Logged
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet 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 IP Logged
yggdrasil
Senior Member
Senior Member
Avatar

Joined: 19 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 150
Quote yggdrasil Replybullet 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 IP Logged
sharona
Senior Member
Senior Member
Avatar

Joined: 16 Oct 2008
Location: United States
Online Status: Offline
Posts: 255
Quote sharona Replybullet 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 IP Logged
Goldberg
Groupie
Groupie
Avatar

Joined: 15 Apr 2011
Location: Portugal
Online Status: Offline
Posts: 58
Quote Goldberg Replybullet Posted: 24 Jun 2011 at 4:52am
Thanks, I guess it is ok now.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.016 seconds.