Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Can you say if then and if then Post Reply Post New Topic
Author Message
Cordellv
Newbie
Newbie
Avatar

Joined: 14 Jan 2009
Location: United States
Online Status: Offline
Posts: 35
Quote Cordellv Replybullet Topic: Can you say if then and if then
     Posted: 14 Jan 2009 at 4:14pm
I am new to Crystal Reports and need help understanding formulas
 
I am trying to add up school employees sick pay. They have Accumulated Sick pay (total 1) , used Sick pay (total 2) and sold Sick pay (total 3).
 
I want to put an if statement that says
 
if  (sick pay field type) = "A"
  then
    Total 1 = Total 1 + (sick pay hours field)
 
and if (sick pay field type) = "U"
   then
      Total 2 = Total 2 + (sick pay hours field)
 
and if (sick pay field type) = "S"
   then
     Total 3 = Total 3 + (sick pay hours field)
 
Then I want to be able to say
 
Grand Total = Total 1 - Total 2 - Total 3
 
Is that possible to do in Crystal reports?  I am getting errors at every turn

Cvail in Seattle
 
Making Things Better One Day At A time
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 14 Jan 2009 at 4:40pm
Hi Cvail,
 
YOu have the right idea, you just need to clean up the syntax a little bit. Try this out:
if  (sick pay field type) = "A"   then
    Total 1 := Total 1 + {yourtable.sickpay}
 
elseif (sick pay field type) = "U"    then
      Total 2 := Total 2 + {yourtable.sickpay}
 
elseif (sick pay field type) = "S"    then
     Total 3 := Total 3 + {yourtable.sickpay};
Try that and see how it works out.
If you are new to Crystal, you might want to check out my Encyclopedia book. I have the entire Crystal syntax documented with sample code. You can find out more about my books at Amazon.com or reading the Crystal Reports eBooks online.
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
Cordellv
Newbie
Newbie
Avatar

Joined: 14 Jan 2009
Location: United States
Online Status: Offline
Posts: 35
Quote Cordellv Replybullet Posted: 14 Jan 2009 at 5:48pm
I borrowed your book from a friend and read it 3 times over Christmas vacation (it is excellent) and purchased it last week from Amazon but have not received it yet.  I will be embarrased if an example of that code is in the book (but I already gave it back so I could not look) 
 
I assumed that ELSE meant INSTEAD OF.  So I see that you are saying that ELSEIF means  IN ADDITION TO.  Is that a command when you put the two words together ELSEIF   instead of ELSE IF?  I thought I had tried that (of the many things I tried) and was getting an error so I may have made a typo that I could not find.  That is what I was trying to do is find some command that  would be the same as saying AND ALSO  ..
 
Thanks for the tip.  I will go try it tomorrow...
 
 
Thanks for putting this Forum here.  It is a great releif to know that when I am totally stumped I can go somewhere for a tip or two.  Now to order the rest of your books....   Cordell
Making Things Better One Day At A time
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 14 Jan 2009 at 6:46pm
cordellv,
Brian cleaned up your syntax but I don't think it will do what you want. You can do an if then else if repeated as in Brian's example above but the elseif is NOT in addition to. The formula will stop as soon as one condition is met. I think you wanted it to do all 3 everytime.
If I understand your original post you need to be able to create the totals per employee and it sounds like each row in your table is identified as A,U or S and has an hour field in it.
 
If I am correct you can handle this by:
Group1 on your Employee field
create 3 running total fields summing your hours field (called Accumulated, Used, Sold as examples) that conditionally uses the "sick pay field type" (1 per type) and reset on group1.
Create a formula field as your Total and use these 3 fields in the formula
something like: (accumulated)-((used)+(sick)) and place this on group footer2. This would give you your total number of hours per employee.
Use the formula field in the group footer (running totals have to be in footers).


Edited by DBlank - 14 Jan 2009 at 6:50pm
IP IP Logged
Cordellv
Newbie
Newbie
Avatar

Joined: 14 Jan 2009
Location: United States
Online Status: Offline
Posts: 35
Quote Cordellv Replybullet Posted: 14 Jan 2009 at 9:10pm
Dblank,
 
You are right.  I had already tried the ELSE IF but as you say, I need to do all three every time as I go down name by name reading each record in the history file to see how much Sick time they have accumulated, used and also sold.  The trouble I am having is that all of the transactions are in the same record.  So I have to use a formula to say if they are type A, U or S so  then  and I cant do a Running total on a Formula field
 
here is the original formula I wrote to try to put them all in one
 
I put this formula in the detail section and supressed the section so it does not print
 
fmSickAccumulate
 
This is what was in that formula

whileprintingrecords;

global numbervar SickTrans;

if {HTOTRN_TRANS.HTODGR-GRP-CODE}='1'

and {HTOTRN_TRANS.HTOTRN-LTD-RECORD} = "N"

and {HTOTRN_TRANS.HTOTRN-TYPE}="A"

and {HTOTRN_TRANS.HPAHDM-ID} <> 0 then

SickTrans := SickTrans + {HTOTRN_TRANS.HTOTRN-HRS}

else

if {HTOTRN_TRANS.HTODGR-GRP-CODE}='1'

and {HTOTRN_TRANS.HTOTRN-LTD-RECORD} = "N" and

{HTOTRN_TRANS.HTOTRN-TYPE}="u" then

SickTrans := SickTrans - {HTOTRN_TRANS.HTOTRN-HRS}

and   (I also tried ELSE here)

if {HTOTRN_TRANS.HTODGR-GRP-CODE}='1'

and {HTOTRN_TRANS.HTOTRN-LTD-RECORD} = "N" and

{HTOTRN_TRANS.HTOTRN-TYPE}="s" then

SickTrans := SickTrans - {HTOTRN_TRANS.HTOTRN-HRS};

 
And I put this formula field in the Footer for Group 1 to actually print on the report
fmSickPrint
 
This is what was in that formula

Whileprintingrecords;

global numbervar SickTrans;

 
Also I then I put this formula in the Group 1 header to reset the total so it would start over with each last name (and the report is grouped by last name)

 

mfSickReset

 

This is what was in that formula

whileprintingrecords;

global numbervar SickTrans := 0

 

 

But it does not reset or add up right as it goes down the records. 

 

It would be so easy in Excel but of course when you are working with a database and have to have selection parameters for date ranges and formulas for limiting the types it gets complex. 

 

It is so amazing that you guys would be willing to take the time to try to help me.  If and when I get this Crystal Reports thing learned, I assure you I will be in here with you helping others....

 

Cordell



Edited by Cordellv - 14 Jan 2009 at 9:16pm
Making Things Better One Day At A time
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 15 Jan 2009 at 7:47am
I am not good at all with shared variables but this wcould be another approach for you to try that is a little less complicated.
create a formula field ("hours" or something similar).
if {HTOTRN_TRANS.HTODGR-GRP-CODE}='1'

and {HTOTRN_TRANS.HTOTRN-LTD-RECORD} = "N"

and {HTOTRN_TRANS.HTOTRN-TYPE}="A"

and {HTOTRN_TRANS.HPAHDM-ID} <> 0 then

{HTOTRN_TRANS.HTOTRN-HRS}

else

if {HTOTRN_TRANS.HTODGR-GRP-CODE}='1'

and {HTOTRN_TRANS.HTOTRN-LTD-RECORD} = "N" and

{HTOTRN_TRANS.HTOTRN-TYPE}="u" then - ({HTOTRN_TRANS.HTOTRN-HRS})

else

if {HTOTRN_TRANS.HTODGR-GRP-CODE}='1'

and {HTOTRN_TRANS.HTOTRN-LTD-RECORD} = "N" and

{HTOTRN_TRANS.HTOTRN-TYPE}="s" then -({HTOTRN_TRANS.HTOTRN-HRS})

This will either make the hours field positive or negative depending on your condition.
Create a summary function summing this formula field at group1 and it should give you the correct total. you can also drop it in the details sectino to validate that it calculating each row correctly.
IP IP Logged
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