Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Statements in a Formula

Status
Not open for further replies.

pticarl

Programmer
Nov 21, 2003
32
0
0
US
This is a newb type question and I really should know the answer to this but I am using CR v7 and I have a formula that uses nested ifs (or how I wish this version supported CASE statements). Based on the criteria of the if statements I would like to execute a block of code for each possibility. The question is, how can I use these multiple command lines in the if statements?

For example

if table1.id = 1 then
statement 1
statement 2
else

if table1.id = 2 then
statement 3
statement 4
etc....

When I try this approach I get an error about the remaining text not being a part of a string, number etc. When drop commands and execute the formula with single lines after the if statements it does not error out.

Any help would be greatly appreciated.

Thanks!



 
Some of this depends on what exactly you're trying to do in the formula. If, for example, statement1 and statement2 give two separate pieces of data that need to be on the report, it will not work. However, if statement1 does something with a piece of data and statement2 does something else with the same piece of data, you might (i.e., I haven't tried it and I'm sure someone will correct me if I'm wrong!) be able to get by with putting parentheses around the statement block and a semicolon between the two statements - something like this:

(statement1;
statement2)

-D
 
A little background is in order then. This Crystal Report is a scheduling assistant for our Production Department. It calculates the daily prodcution requirements at each machine center and also maintains running totals from day to day. Since I have 2 nested groups, the first by date and the second by each machine process, I have to use variables to pull the running totals from each daily grouping by machine center. Then the if statement would add the current daily hours by machine center to the appropriate variable holding the accumulated hours. The second statement sets a sentinel flag so I know if that particular machine center was scheduled on that particular day (so I can add back in hours if it was unscheduled, which is done in the daily group footer).

I will give the ( statement; statement ) format a try and will get back to you.

Thanks!

 
; ends the command.

You won't be able to use a ; and then stick an ELSE clause in for the IF after you've already closed the command.

You get an error and cursor placement at Statement4, because it isn't prefixed with an expected condition of an IF statement.

You have to use either THEN, ELSE or ;IF between each criteria of a nested IF.

Naith
 

I pretty much thought that that was the case with Crystal Reports. I can run the report without using the multi-line nested IFs by using seperating the functions of the formulas, but that was the more elegant solution to the problem.

Thanks everyone for your answers and stars for both of you!

 
What I described isn't just the case with Crystal Reports, it's your standard IF/THEN/ELSE logic for all 3/4GLs.

If you want to go with nested IFs or a CASE statement, but are not sure how to go about it, then if you actually paste in your code, I'm sure someone can knock something up for you quickly enough.

Naith
 
Maybe we are talking about different things here but using multiple lines in IF/Else control structures is common in many languages, don't nearly all of them support the use of code blocks inside IF/Else structures?

For example here are a few lines of code that I picked at random from a VB .Net app I have...

Code:
If blnFileError = False Then 
    objexcel.Range("H46").Font.Bold = True
    objexcel.Range("H46").Font.Size = 24
    objexcel.Range("H46").Value2 = 12
    objexcel.DisplayAlerts = True
Else
    objexcel.Range("H46").Font.Bold = True
    objexcel.Range("H46").Font.Size = 24
    objexcel.Range("H46").Value2 = 12
    objexcel.DisplayAlerts = True
End IF

CASE/SELECT statements also allow processing of code blocks inside each decision point.

That is the capability that I would like to have in the formula in my nested IF/Else statement in Crystal. Maybe I have something wrong in my syntax.

Thanks!
 
No, you make a valid point there. I should have said 4GL not 3 and 4GL.

Whilst the syntax you have illustrated is cool for a 3GL like VB, in SQL and indeed in Crystal, you would have to link all the statements, like:

If blnFileError = False Then
objexcel.Range("H46").Font.Bold = True and
objexcel.Range("H46").Font.Size = 24 and
objexcel.Range("H46").Value2 = 12 and
objexcel.DisplayAlerts = True

The concept is pretty similar, but the syntax is slightly different.

The above is an example, and wouldn't be portable to Crystal. You couldn't set font types, colours, and sizes in Crystal unless you interpreted the formula as html.

Naith
 
Makes sense then. I tried to add in the "AND" to link multiple statements in the IF/ELSE block with I still receive an error.

Here is an example of my current nested IF/ELSE formula

Code:
IF {wc} = "AP" THEN 
   shared numbervar ap:=shared numbervar ap + 
   ({@hours_avail_by_work_center} - {#hours_required}) 
ELSE
    IF {SFDTLFIL_SQL.wc} = "FA" THEN 
    shared numbervar fa:=shared numbervar fa + 
    ({@hours_avail_by_work_center} - {#hours_required})
ELSE
0

What I would like to do is add a line for each nested IF to set a boolean sentinel flag to true like below.

Code:
shared booleanvar akf:=true

I tried
Code:
IF {wc} = "AP" THEN 
   shared numbervar ap:=shared numbervar ap + 
   ({@hours_avail_by_work_center} - {#hours_required}) 
    AND
    shared booleanvar akf:=true
ELSE

But that gave me an error message expecting a boolean variable even though it is properly defined and accessible from other formulas in the same scope block of the report.

I am going to give this one last whack and call it a day on this report. It works already but I think using this approach would result in a "cleaner" report.

Thanks for your help naith!
 
You're nearly there. ":=" is an assignment. "=" is a boolean comparison.

What you want, although I'm sure you've figured it out now, is:

AND
shared booleanvar akf = true

Naith

PS: You don't need to keep declaring the variable type after the initial time. Once you declare akf, for example, as a shared booleanvar, you can subsequently refer to the variable within the same formula simply as 'akf'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top