so, i'm trying to generate a IF Then in crystal reports, basic syntax (i will use crystal syntax if that's what you give me). i have a value coming back from a stored procedure ranging from 0 to 4. depending on that number, i want to return back different phrases.
right now what i got:
formula = ""
IF {sp_cr_getSubsequentVisitBySubVisitID;1.Judgement} = 1 THEN
formula = "poor"
end if
if i change the ' 1 ' to ' true ', it'll save it, but it comes up with an error if i use ' 1 ' or ' "1" '. I've also tried using == and 1 = {sp_cr....}
Thanks for the help!
Crystal syntax, and Basic syntax AFAICT, require that you declare the variable to which you would like to assign a value. This is a tested example (my test field returns int as well):
stringVar formula := "";
if {sp_cr_getSubsequentVisitBySubVisitID;1.Judgement} = 1 then
formula := "completed"
else
formula := "n/a"
PS: don't forget the semicolon after the variable declaration.
Related
I have a field in crystal reports that displays customer's names. the requirement however is to just display the first 5 characters, which includes spacing as well. whatever comes after the 5th character will be converted into * and the spacing shall be displayed as a space still. as the name field varies, how do i accomplish this?
currently i only have this, which displays the first 5 characters and replaces only the 6th character with an *
Replace({Command.CUST_NAME},MID({Command.CUST_NAME},6),"*")
Unfortunately, it seems Crystal does not suport regex.
So, i would do a function. Try this:
stringvar input := {Command.CUST_NAME};
stringvar output := MID(input, 1, 5);
numbervar i;
for i:=5 to Length (input) step 1 do (
stringvar aChar := MID(input, i, 1);
if aChar <> " " then aChar := "*";
output := output + aChar;
);
output
Maybe you will have to handle if {Command.CUST_NAME} is smaller than 5 chars.
I am creating a report by Crystal Reports which will mask address field depending upon parameter set to the report. I know this can be done using formula. I can successfully mask with hard coded value. However I need something like password masking. For example, if address is D/302 ABC apartment, it should be displayed as X/XXX XXX XXXXXXXXX. Only characters and numbers to be masked space and special characters not be masked. In addition length of masked data should match actual data.
I think you can use a formula like this:
Local StringVar str := "";
Local NumberVar strLen := Length({User.Address});
Local NumberVar i;
For i := 1 To strLen Do
(
if (ChrW({User.Address}[i]) in (AscW("A") to AscW("Z"))) or (ChrW({User.Address}[i]) in (AscW("a") to AscW("z"))) or (ChrW({User.Address}[i]) in (AscW("0") to AscW("9"))) Then
str := str + "X"
else
str := str + {User.Address}[i];
);
str
Logic is correct.. I have done the same thing by using Mid function instead of chrW (). No need to use asw () either. Simply use range operator.
Logic is correct.. I have done the same thing by using Mid function instead of chrW (). Additional you can use formula as ReplicateString ("X",len(address));
Only problem is space will be also masked
I am writing some formulas in crystal reports. I have a field that may contain a numeric value, in which case I am doing some calulation on it, or it may contain a string value, in which case it should be returned verbatim. So I thought this should work: If Result.entry contains a number, then put that numeric value into Result, else just return Result.entry:
Local NumberVar Result := 0;
if NumericText({RESULT.ENTRY}) // may be N.D. or B.L.D.
then
( Result:=val({RESULT.ENTRY});)
else (
{RESULT.ENTRY} );
);
// something more going on here that at last returns some value
But no..."A number is required here" says CR and highlights the block following else... Any clue at all to what Crazy, sorry Crystal Reports wants here and why this is not acceptable? I've even tried to set a value and report that afterwards, (as per Crystal report if then help, how to return a string or variable with formula) but I still get the same error.
else ( 0 );
works - but that is definately not what I want. (the other return value from this function is also a string)
you are trying to assign if part as number and else part as String which won't work, you either return both numbers or both strings.
So you save the numeric value as string and while using in calculation convert to number
Local StringVar Result;
if NumericText({RESULT.ENTRY}) // may be N.D. or B.L.D.
then
Result:={RESULT.ENTRY};
else (
{RESULT.ENTRY} );
);
Now when using in calculation you can use ToNumber(Result)
Almost important to note here is that if declaring something as NumberVar then the field being added has to be a number. If dealing with decimals use CurrencyVar as you cannot set NumberVar to 0 and then add it to a decimal value, which will result in getting a number is expected here result.
Similar to my first question. I want to show my address in a text box containing the fields as follows
{Company}
{AddLine1}
{AddLine2}
{ZIP}{State}{City}
{Country}
The line that concerns me (and hopefully some of you guys) is {ZIP} {City} {State}. What I want to produce is a consistent addressing format, so that there will be no blank space or indentation even if a ZIP, City or State field has been left blank in the DB. This line should still line up with the rest of the rows and not be indented. I also wish to insert commas between zip, state, city where they are relevant and leave them out where not. For this I have written a formula. Below:
If isnull({BILL_TO.ZIP}) or trim({BILL_TO.ZIP})= "" Then "" else {BILL_TO.ZIP}
+
(If isnull({BILL_TO.State}) or trim({BILL_TO.State})= "" Then ""
else(If not isnull({BILL_TO.ZIP}) and length(trim({BILL_TO.ZIP})) <> 0 Then ", " else "") + {BILL_TO.State})
+
(If isnull({BILL_TO.CITY}) or length(trim({BILL_TO.CITY})) = 0 Then ""
else(
If (not isnull({BILL_TO.State}) and length(trim({BILL_TO.State})) <> 0)
or
(not isnull({BILL_TO.Zip}) and length(trim({BILL_TO.Zip})) <> 0)
Then ", " else "")+ {BILL_TO.CITY}))
The problem is that when there is only a city (no state or zip entered) the formula itself will not display. It does however display when the others are present.
Can anyone see a bug in this code??? Its killing me
Thanks for the help.
Look forward to hearing from you guys!
There are a whole lot of if-then-elses going on in that formula so it's hard to tell, but if it's not displaying anything that probably means that you're using a field somewhere without handling its null condition first. Something simpler might be your best bet:
local stringvar output;
if not(isnull({Customer.Postal Code})) then output:=trim({Customer.Postal Code}) + ', ';
if not(isnull({Customer.Region})) then output:=output + trim({Customer.Region}) + ', ';
if not(isnull({Customer.City})) then output:=output + trim({Customer.City}) + ', ';
left(output,length(output)-2)
Create a formula field for each database field. For example:
// {#CITY}
If Isnull({BILL_TO.CITY}) Then
""
Else
Trim({BILL_TO.CITY})
// {#STATE}
If Isnull({BILL_TO.STATE}) Then
Space(2)
Else
Trim({BILL_TO.STATE})
// {#ZIP}
If Isnull({BILL_TO.ZIP}) Then
Space(5) //adjust to meet your needs
Else
Trim({BILL_TO.ZIP})
Embed each formula field in a text object.
Format text object and its fields to meet your need.
** edit **
I you have data-quality issues, address them in the STATE and ZIP formulas (because they will be a constant length). I would add the commas and spacing the text object.
There are hints of the answer to this question here and there on this site, but I'm asking a slightly different question.
Where does Crystal Reports document that this syntax does not work?
Trim({PatientProfile.First}) + " "
+ Trim(Iif(
IsNull({PatientProfile.Middle})
, Trim({PatientProfile.Middle}) + " "
, " "
)
)
+ Trim({PatientProfile.Last})
I know the solution is
If IsNull({PatientProfile.Middle}) Then
Trim({PatientProfile.First})
+ " " + Trim({PatientProfile.Last})
Else
Trim({PatientProfile.First})
+ " " + Trim({PatientProfile.Middle})
+ " " + Trim({PatientProfile.Last})
but how are we supposed to figure out we can't use the first version?
The documentation for IsNull says
Evaluates the field specified in the current record and returns TRUE if the field contains a null value
and Iif gives
[Returns] truePart if expression is True and falsePart if expression is False. The type of the returned value is the same as the type of truePart and falsePart.
I suppose if you stare at that line about "type of the return value" you can get it, but...
Where does Crystal Reports document that this syntax does not work?
I doubt there is anyplace large enough in the entire universe to document everything that does not work in Crystal Reports...
I know I'm years late on this one, but I came upon this question while trying to figure out the same thing. Funny enough, I couldn't even find the answer in Crystal Reports documentation, but instead in a link to IBM.
Baiscally, if you're using Crystal Reports 8.x or 10.x, ISNULL and IIF don't work together. From the site:
Cause
There is a defect in Crystal Reports 8.x and 10.x that prevents the above formula from working correctly. The 'IIF' and 'IsNull' commands cannot function together, and that includes attempting to use "Not" to modify the IsNull command; for example, IIF(Not IsNull ()).
Resolving the problem
The workaround is to use an "If-Then-Else" statement. For example,
If IsNull({~CRPT_TMP0001_ttx.install_date}) Then "TBD" Else "In Progress"
So if you're using CR 8.x or 10.x (which we are), you're out of luck. It makes it REAL fun when you are concatenating multiple fields together and one of them might be NULL.
I think CR evaluates both IIFs true and false parts. Because you have "Trim({PatientProfile.Middle})" part there, which will be evaluated aganst null value, CR formula evaluator seems just fail.
try this:
currencyvar tt;
currencyvar dect;
tt :={ship.comm_amount};
dect := tt - Truncate(tt);
tt := truncate(tt);
dect := dect * 100;
if dect = 0 then
UPPERCASE('$ ' + ToWords (tt,0 )) + ' ONLY'
else
UPPERCASE('$ ' + ToWords (tt,0) + ' And ' + ToWords(dect,0)) + ' ONLY ';