INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Nested If Else

## Nested If Else

(OP)
Good Afternoon Experts!

I have a text field that I only want to print certain lines of. The start line is fixed as line 4, however the last line is dependant of where a certain string sits within the text field afetr line 6. The formula below is what I want to acheive but Crystal is telling me that from the first 'else', that the text does not appear to be part of the formula. I'm pretty sure that this is going to be a parenthesis issue and I've tried several changes but to no avail....any help please? Thanks Seteve

numbervar i;
stringvar Result;
for i:= 4 to {@RowCount} do
if i < 6 then
result := result + x[i];
else
if i > 6 then
if instr(x[i], '==========') > 0 then
i = {@RowCount};
else
result := result + x[i];
else
result := result + x[6];

Result;

### RE: Nested If Else

If it is a nested if issue, try:

numbervar i;
stringvar Result;
for i:= 4 to {@RowCount} do
if i < 6 then
result := result + x[i];
else if i > 6 then
(
if instr(x[i], '==========') > 0 then
i = {@RowCount};
else
result := result + x[i];
)
else
result := result + x[6];

result;

### RE: Nested If Else

Also:

maybe this:
i = {@RowCount};
needs to be:
i := {@RowCount};

### RE: Nested If Else

(OP)
Hi Andy

Thanks for the response and apoligies for the delay in mine, I'be been sunning myself!

I've tried your solution but I'm getting an error on the formula compile. It's stating that from the 'else if i > 6 then' inclusive, the remaining text does not appear to be part of the formula.

I've tried various changes but I can't see what's wrong with it!

Any help will be greatly appreciated.

Thanks

Steve

### RE: Nested If Else

I believe the semi-colon at i = {@RowCount}; should be removed. The semi-colon rules can be confusing.

(OP)
Hi Kray4660

Cheers

Steve

### RE: Nested If Else

Did a little bit of experimenting and came across a big issue.

if instr(x[i], '==========') > 0 then
i := {@RowCount}
else
result := result + x[i]

The if statement has to 'return' the same data type. You are setting an integer to a value and a string to a value. I do not remember if you can get around this by using Basic syntax.

Also your for loop is not set up correctly for Crystal Syntax

For i:= 4 to {@RowCount} Do
(
Statements
)

### RE: Nested If Else

I reworked the formula into Basic syntax. I do not know if it works correctly, but at least no syntax errors.

Dim x() as string
x = split({Command.resourcelastname},chr(10))
dim i as number
dim Result as string

for i= 4 to {@RowCount}
if i < 6 then
result = result + x(i)
else
if i > 6 then
if instr(x(i), "==========") > 0 then
i = {@RowCount}
else
result = result + x(i)
end if
else
result = result + x(6)
end if
end if

next i
formula = result

### RE: Nested If Else

(OP)
Hi Kray4660

I came to the same conclusion as you regaring the 'type' issue within the formaula.

I've replaced the formaula with what you have just supplied and I do get a sysntax error....it states that the whole formula does not appear to be part of the formula....am I missing the point here?

Steve

### RE: Nested If Else

I forgot in testing I replaced the one field name with an existing one that I had in a database.

Dim x() as string
dim i as number
dim Result as string

for i= 4 to {@RowCount}
if i < 6 then
result = result + x(i)
else
if i > 6 then
if instr(x(i), "==========") > 0 then
i = {@RowCount}
else
result = result + x(i)
end if
else
result = result + x(6)
end if
end if

next i
formula = result

See if this works better. FYI: I did not get any syntax errors. Be sure to use the Basic syntax.

### RE: Nested If Else

I amended your formula as follows:

#### CODE

stringvar array x := split({v_extradatacomputers1.TaskList SBS},chr(10));
numbervar i;
stringvar Result;
for i:= 4 to {@RowCount} do
(
IF      i < 6
THEN    Result := Result + x[i]
ELSE
IF      i > 6
THEN    IF      instr(x[i], '==========') > 0
THEN    (i := {@RowCount}; '')
ELSE    Result := Result + x[i]
ELSE    Result := Result + x[6]
);
Result; 

It is accepted and does return data. Not absolutely certain the result is what you want but hopefully it gets you closer.

Cheers
Pete

### RE: Nested If Else

(OP)
Thanks Pete

I got it to work, had to add Result := ''; as it was concatenating acroos records!

Cheers

Steve

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!