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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with nested if...

Status
Not open for further replies.

celauritz

Technical User
Dec 2, 2005
41
US
I have this formula. It's pretty straight forward, but Crystal is telling me that I need a closed parenthesis and is highlighting the word "else" in line nine.

I know one doesn't need to go there. I'm thinking I have a syntax error. If you need any more info about the fields, let me know.

Basically, it looks at DROPP and depending on what it says, it looks at ADDP, and then DROPC and then ADDC. There are possibilities that the fields could contain information not in the formula, and in that case, the output should be zero.

Code:
if isnull({Command.DROPP}) then
    (if {Command.ADDP} = "001" then
        (if isnull({Command.DROPC}) then
            (if isnull({Command.ADDC}) then 10 else
            if {Command.ADDC} = "BD3" then 35 else 0)
        else 0) else
        (if {Command.DROPC} = "B1" then 
            (if {Command.ADDC} = "BD3" then 30 else 0)
        else 0) else
        (if {Command.DROPC} = "B2" then
            (if {Command.ADDC} = "BD3" then 30 else 0)
        else 0) else
        (if {Command.DROPC} = "B3" then
            (if {Command.ADDC} = "BD3" then 10 else 0)
        else 0) else
        (if {Command.DROPC} = "BD1" then
            (if {Command.ADDC} = "BD3" then 30 else 0) 
        else 0) else
        (if {Command.DROPC} = "BD2" then
            (if {Command.ADDC} = "BD3" then 30 else 0)
        else 0) else
    0) else
    (if {Command.ADDP} = "002" then
        (if isnull({Command.DROPC}) then
            (if isnull({Command.ADDC}) then 10 else
            if {Command.ADDC} = "BD3" then 35 else 0)
        else 0) else
        (if {Command.DROPC} = "B1" then 
            (if {Command.ADDC} = "BD3" then 30 else 0)
        else 0) else
        (if {Command.DROPC} = "B2" then 
            (if {Command.ADDC} = "BD3" then 30 else 0) 
        else 0) else
        (if {Command.DROPC} = "B3" then 
            (if {Command.ADDC} = "BD3" then 10 else 0)
        else 0) else
        (if {Command.DROPC} = "BD1" then 
            (if {Command.ADDC} = "BD3" then 30 else 0)
        else 0) else
        (if {Command.DROPC} = "BD2" then 
            (if {Command.ADDC} = "BD3" then 30 else 0)
        else 0) else
    0) else
    (if {Command.ADDP} = "003" then
        (if isnull({Command.DROPC}) then 
            (if isnull({Command.ADDC}) then 5 else
            if {Command.ADDC} = "BD3" then 25 else 0) 
        else 0) else
        (if {Command.DROPC} = "B1" then 
            (if {Command.ADDC} = "BD3" then 25 else 0) 
        else 0) else
        (if {Command.DROPC} = "B2" then 
            (if {Command.ADDC} = "BD3" then 25 else 0) 
        else 0) else
        (if {Command.DROPC} = "B3" then 
            (if {Command.ADDC} = "BD3" then 5 else 0) 
        else 0) else
        (if {Command.DROPC} = "BD1" then 
            (if {Command.ADDC} = "BD3" then 25 else 0)
        else 0) else
        (if {Command.DROPC} = "BD2" then 
            (if {Command.ADDC} = "BD3" then 25 else 0)
        else 0) else
    0) else 0) else
(if {Command.DROPP} = "003" then
        (if {Command.ADDP} = "001" then 5 else
        if {Command.ADDP} = "002" then 5) else 0)
 
I believe the code below will do what you are looking for. I tried to clean it up some without changing your overall layout much. In my experience all of the parenthesis are not necessary. They just make it more difficult because you have to match them. I prefer to just stick to a "tabbing pattern". (Although after previewing this it becomes apparent that the layout shifts when you preview/submit your answer - sorry, my "else 0" should be over a tab w/ the exception of the second one when there are 2 in a row)
You also could have used a select case statement to "split up" your code - not necessary either. I hope I understood what your were going for and this works for you.

if isnull({Command.DROPP}) then
if {Command.ADDP} = "001" then
if isnull({Command.DROPC}) then
if isnull({Command.ADDC}) then 10
else if {Command.ADDC} = "BD3" then 35
else 0
else if {Command.DROPC} = "B1" then
if {Command.ADDC} = "BD3" then 30
else 0
else if {Command.DROPC} = "B2" then
if {Command.ADDC} = "BD3" then 30
else 0
else if {Command.DROPC} = "B3" then
if {Command.ADDC} = "BD3" then 10
else 0
else if {Command.DROPC} = "BD1" then
if {Command.ADDC} = "BD3" then 30
else 0
else if {Command.DROPC} = "BD2" then
if {Command.ADDC} = "BD3" then 30
else 0
else 0
else if {Command.ADDP} = "002" then
if isnull({Command.DROPC}) then
if isnull({Command.ADDC}) then 10
else if {Command.ADDC} = "BD3" then 35
else 0
else if {Command.DROPC} = "B1" then
if {Command.ADDC} = "BD3" then 30
else 0
else if {Command.DROPC} = "B2" then
if {Command.ADDC} = "BD3" then 30
else 0
else if {Command.DROPC} = "B3" then
if {Command.ADDC} = "BD3" then 10
else 0
else if {Command.DROPC} = "BD1" then
if {Command.ADDC} = "BD3" then 30
else 0
else if {Command.DROPC} = "BD2" then
if {Command.ADDC} = "BD3" then 30
else 0
else 0
else if {Command.ADDP} = "003" then
if isnull({Command.DROPC}) then
if isnull({Command.ADDC}) then 5
else if {Command.ADDC} = "BD3" then 25
else 0
else if {Command.DROPC} = "B1" then
if {Command.ADDC} = "BD3" then 25
else 0
else if {Command.DROPC} = "B2" then
if {Command.ADDC} = "BD3" then 25
else 0
else if {Command.DROPC} = "B3" then
if {Command.ADDC} = "BD3" then 5
else 0
else if {Command.DROPC} = "BD1" then
if {Command.ADDC} = "BD3" then 25
else 0
else if {Command.DROPC} = "BD2" then
if {Command.ADDC} = "BD3" then 25
else 0
else 0
else 0
else if {Command.DROPP} = "003" then
if {Command.ADDP} = "001" then 5
else if {Command.ADDP} = "002" then 5
else 0
else 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top