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!

Nested if-then-else formula won't display everything 1

Status
Not open for further replies.

TiggerMom

Programmer
Dec 13, 2000
15
CA
When I use the following formula, only the first "if" is displayed in my report while the remaining 3 if pieces are not displaying anything. I am using this formula to create a grouping level in my report. When I change the order of the 4 if sections, only the first if section displays data. I think there is something wrong in my formula but can't see what it is. I am using Crystal 7 and I'm connected using SQLl/ODBC connection with tables that were created back in the 1980's in DB2. The field {PHORD.GENERIC} is a generic name for a drug. I am working with a Pharmacy database and this formula is grouping the records into Daily Dosage groups based on how many MG of drug the patient receives per day. Your help is GREATLY appreciated!

FORMULA:
*********
If {PHORD.GENERIC} = "Olanzapine" Then
If {@DailyDosage} >= 0 and {@DailyDosage} <= 10 Then
ToText(&quot;0 - 10 MG&quot;)
Else If {@DailyDosage} >= 11 and {@DailyDosage} <=20 Then
ToText(&quot;11 - 20 MG&quot;)
Else If {@DailyDosage} >= 21 and {@DailyDosage} <=30 Then
ToText(&quot;21 - 30 MG&quot;)
Else If {@DailyDosage} >= 31 Then
ToText(&quot;> 30 MG&quot;)
Else
If {PHORD.GENERIC} = &quot;Clozapine&quot; Then
If {@DailyDosage} >= 0 and {@DailyDosage} <= 300 Then
ToText(&quot;0 - 300 MG&quot;)
Else If {@DailyDosage} >= 301 and {@DailyDosage} <=600 Then
ToText(&quot;301 - 600 MG&quot;)
Else If {@DailyDosage} >= 601 Then
ToText(&quot;> 600 MG&quot;)
Else
If {PHORD.GENERIC} = &quot;Quetiapine&quot; Then
If {@DailyDosage} >= 0 and {@DailyDosage} <= 399 Then
ToText(&quot;0 - 399 MG&quot;)
Else If {@DailyDosage} >= 400 and {@DailyDosage} <= 800 Then
ToText(&quot;400 - 800 MG&quot;)
Else If {@DailyDosage} >= 801 Then
ToText(&quot;> 800 MG&quot;)
Else
If {PHORD.GENERIC} = &quot;Risperidone&quot; Then
If {@DailyDosage} >= 0 and {@DailyDosage} <= 4 Then
ToText(&quot;0 - 4 MG&quot;)
Else If {@DailyDosage} >= 5 and {@DailyDosage} <=6 Then
ToText(&quot;5 - 6 MG&quot;)
Else If {@DailyDosage} >= 7 Then
ToText(&quot;> 6 MG&quot;)

 
Crystal doesn't have an EndIf, so unless you define 2 ELSE lines back to back at the end of the loop, it will put the second group as part of the first groups else.

Try the following at the end of the &quot;Olanzapine&quot; loop.

Then
ToText(&quot;21 - 30 MG&quot;)
Else ToText(&quot;> 30 MG&quot;)
Else
If {PHORD.GENERIC} = &quot;Clozapine&quot; Then Ken Hamady
On-site custom Crystal Reports Training and Consulting.
Quick Reference Guide to using Crystal in VB.
 
Note that the same issue applies to the other major &quot;groups&quot; in this statement. Also, it would be a bit simpler to restructure the order of the statement, such as:
If {PHORD.GENERIC} = &quot;Olanzapine&quot; Then
(
If {@DailyDosage} >= 31 Then
ToText(&quot;> 30 MG&quot;)
Else If {@DailyDosage} >= 21 Then
ToText(&quot;21 - 30 MG&quot;)
Else If {@DailyDosage} >= 11 Then
ToText(&quot;11 - 20 MG&quot;)
Else If {@DailyDosage} >= 0 Then
ToText(&quot;0 - 10 MG&quot;)
Else
&quot;daily dosage unknown&quot;
)
Else If {PHORD.GENERIC} = &quot;Clozapine&quot; Then ....
I also generally use parentheses for nested if-then-else statements, as I find they improve readability. If you use parentheses as above, the ELSE statement isn't required, but you might want to have some sort of error trapping there in case you have a negative dosage or something weird like that.
Malcolm Wynden
I'm for sale at malcolm@wynden.net
 
Thanks! Your tips worked great.
TiggerMom is purrin'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top