Solved

# Formula with variables error. I hate semicolons!

Posted on 2007-10-17
332 Views
I am using Crystal Reports XI to write a formula using variables to find the tot hours worked, regular hours worked, and overtime hours worked but I keep getting an error. I know it has to do with the placement of my semicolons but cannot figure it out. Below is the code for my formula:

WhilePrintingRecords;
//Find the hours worked that shift
numberVar hours := round({@TOut} - {@TIn}/3600,2);
//See if the employee is the same
If {PR_EMP.ID} = Previous({PR_EMP.ID}) Then
//Find the total hrs worked
numberVar tot_hrs  := tot_hrs + hours;
//Find out if the total hours is greater than 40
If tot_hrs > 40 then //If the total hours are greater than 40
//Set the regular hours to 40
numberVar reg_hrs := 40
//Find out the overtime hrs (Total Hours - 40)
numberVar ot_hrs := tot_hrs - 40;
Else //The total hours is less than 40
//Set the regular hours equal to total hours
numberVar reg_hrs := tot_hrs
//Set overtime hours = 0
numberVar ot_hrs := 0;
Else //New Employee Record
//Total hours equals hours worked
numberVar tot_hrs := hours
//Regular hours equals total hours
numberVar reg_hrs := tot_hrs
//Set overtime hours to 0
numberVar ot_hrs := 0;
hours;

The error message read: The remaining text does not appear to be part of the formula.

Any help would be appreciated.
0
Question by:bhorlings

LVL 100

Accepted Solution

Actually it is more complex than that.  Crystal uses the ; to end a statement and if you need to execute more than 1 statement in a block like the THEN clause you use ( )

WhilePrintingRecords;
//Find the hours worked that shift
numberVar hours := round({@TOut} - {@TIn}/3600,2);
//See if the employee is the same
If {PR_EMP.ID} = Previous({PR_EMP.ID}) Then
(                                                                 //If executes 2 statements
//Find the total hrs worked
numberVar tot_hrs  := tot_hrs + hours;
//Find out if the total hours is greater than 40
If tot_hrs > 40 then //If the total hours are greater than 40
(
//Set the regular hours to 40
numberVar reg_hrs := 40;            //Semicolon to end the statement
//Find out the overtime hrs (Total Hours - 40)
numberVar ot_hrs := tot_hrs - 40;
)                                                        //No semicolon because that would end the IF
Else //The total hours is less than 40
(                                                                  //Else executes several statements
//Set the regular hours equal to total hours
numberVar reg_hrs := tot_hrs;              //Semicolon
//Set overtime hours = 0
numberVar ot_hrs := 0;
)
)
Else //New Employee Record
(
//Total hours equals hours worked
numberVar tot_hrs := hours;                    //Semicolon
//Regular hours equals total hours
numberVar reg_hrs := tot_hrs;                    //Semicolon
//Set overtime hours to 0
numberVar ot_hrs := 0;
);                                                                     //Semicolon
hours                                                          //Semicolon not required there is no statement after this

I hope the explanation helps.  If you have other questions just ask.

mlmcc
0

LVL 77

Expert Comment

WhilePrintingRecords;
numberVar tot_hrs;
numberVar reg_hrs;
numberVar ot_hrs ;
//Find the hours worked that shift
numberVar hours := round({@TOut} - {@TIn}/3600,2);
//See if the employee is the same
If {PR_EMP.ID} = Previous({PR_EMP.ID}) Then
(
tot_hrs  := tot_hrs + hours;
//Find out if the total hours is greater than 40
If tot_hrs > 40 then //If the total hours are greater than 40
//Set the regular hours to 40
(       reg_hrs := 40;
//Find out the overtime hrs (Total Hours - 40)
ot_hrs := tot_hrs - 40)
Else //The total hours is less than 40
//Set the regular hours equal to total hours
(  reg_hrs := tot_hrs;
//Set overtime hours = 0
numberVar ot_hrs := 0)
)
Else //New Employee Record
//Total hours equals hours worked
( tot_hrs := hours;
//Regular hours equals total hours
reg_hrs := tot_hrs;
//Set overtime hours to 0
numberVar ot_hrs := 0);
hours;
0

LVL 77

Expert Comment

half asleep....
0

## Featured Post

Hi, In my previous Article  (http://www.experts-exchange.com/Database/Reporting/A_15199-Introduction-to-Microstrategy.html)I discussed some basic understanding of Microstrategy that how we can get in Intro of Microstrategy (MSTR). Now it's tim…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…