NiksMan
NiksMan

Reputation: 21

Suppress or write text if condition is met formula crystal reports

I have a text object field in a subreport, where I need to evaluate value given from DB, and if that value match any of given options, that text will be written (each option has different text). Since it is in a box, I need it to be suppressed so it could look ok. Something like this:

    If {MyDB.ValueId} = 1 then 'write text one'
      else (should suppress since no text is needed)
    If {My.DB.ValueId} = 2 then 'write text two'
      else (should suppress since no text is needed)
    If {My.DB.ValueId} = 2 then 'write text two'
      else (should suppress since no text is needed)

I wrote the formula beside suppress option, and it can not be saved since it expects boolean value (true or false, not some text). Any idea how to do that? Tnx in advance..

Upvotes: 0

Views: 4637

Answers (3)

Jeremy Peck
Jeremy Peck

Reputation: 156

I have a couple of thoughts here: A) if the field is being suppressed on the sub report then what R. McMillan wrote should work for you.

B) if the field is being suppress on the main report then it won't. You will need to utilize variables to carry the information from the sub report to the main report.

Shared numberVar test1;
test1 := {@statement}
test1

On the main report you would duplicate the formula, and use the formula to create any suppression formula you need from there (I use things like this to suppress sub reports).

Also I would use a case statement instead of the in-then:

select {db.field}
case <condition1> : <do thing 1>
case <condition2> : <do thing 2>
default : <do thing 3>

its a shorter statement that I have found is much easier to maintain.

Upvotes: 0

R. McMillan
R. McMillan

Reputation: 1424

You're going about this the wrong way. You can't handle the printing of text and the suppression of fields/sections all within the same formula. You need one formula to handle the text, and then a second formula to handle the suppression.

For the text, the formula would be something like:

If {MyDB.ValueId} = 1 then 'write text one'
If {My.DB.ValueId} = 2 then 'write text two'
If {My.DB.ValueId} = 3 then 'write text three'

As suggested, a Case statement could also be used and would be better for proper coding practices. The multiple If statements will work in this example, but its a bad habit to get into because if there were a scalar formula, like one used in a Suprress property, it would result in errors because it could return more than 1 value. The Case statement suggested by MilletSoftware is a better code structure because it follows good coding practices and returns only a single value.

As for the suppression formula, you will want to use the same formula almost, but instead of printing text when the value you are evaluating is true, you will want to specify a boolean value that indicates if the field or section should be suppressed. This would be something like:

If {MyDB.ValueId} In [1, 2, 3] then False
Else True

If this formula is used in the Suppress property of a field or section, then it will show the field or section when MyDB.ValueId is 1, 2, or 3; while suppressing the field or section for all other values. You should be mindful of how it will handle nulls though. Sometimes nulls can be unpredictable when they are not handled specifically. However, this formula shouldn't have any issues with them.

Upvotes: 0

MilletSoftware
MilletSoftware

Reputation: 4001

Select {MyDB.ValueId}
Case 1: 'text one'
Case 2: 'text two'
Default: "" ; 

Upvotes: 1

Related Questions