Crystal report "a number is required here" - crystal-reports

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.

Related

string and date time error message - when using on the same formula-field

I am new to CR and trying to get an invoice formula to read sometimes as a date and sometimes as a text. This depends on the "terms code" for the customer how they should pay: there are only 4 codes, and sometimes it is blank.
I tried with different syntax using
str() or totext
but I do not know the right formatting. See below for my attempt :)
if isnull({PAYTERMS.TERMSCODE}) then {INVOICE.DATE}
else if {PAYTERMS.TERMSCODE}=1 then {INVOICE.DATE}+30
else if {PAYTERMS.TERMSCODE}=2 then {INVOICE.DATE}+45
else if {PAYTERMS.TERMSCODE}=3 then {INVOICE.DATE}
else if {PAYTERMS.TERMSCODE}=4 then "Upon Receipt"
else {INVOICE.DATE}
It keeps returning an error:
a date is required here
The result of the formula must be of the same datatype, you can't mix dates and strings.
So you have to convert the dates to string.
There's no str()-function in Crystal Reports as far as I know. I guess you mean cstr().
It does not matter if you use the cstr() or totext() because totext() is just an alias for cstr().
The formula should look as follows:
if isnull({PAYTERMS.TERMSCODE}) then cstr({INVOICE.DATE})
else if {PAYTERMS.TERMSCODE}=1 then cstr({INVOICE.DATE}+30)
else if {PAYTERMS.TERMSCODE}=2 then cstr({INVOICE.DATE}+45)
else if {PAYTERMS.TERMSCODE}=3 then cstr({INVOICE.DATE})
else if {PAYTERMS.TERMSCODE}=4 then "Upon Receipt"
else cstr({INVOICE.DATE})
Additionally you can set the second argument of cstr() to define the output format of the date:
cstr({INVOICE.DATE}, "MM/dd/yyyy") // Output will be like "03/26/2019"

Mask address in Crystal Reports

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

How to split a string and make an array of integers in crystal report

I have a string with set of employee IDs separated by an _(Underscore).
What i want to do is to split that into separate strings and convert them to integers and save them in an integer array.
Is this possible in Crystal Reports?
Is it possible to convert string into integer/number in Crystal Report?
I have tried using split function but still couldn't figure out how to use it to create an array.
I am very new to crystal report it would be a great help if you could help me out.
//create an array of strings by parsing a underscore-delimited string field
Stringvar Array strings := Split({table.string_field}, "_");
//empty numeric array; sized to match
Numbervar Array numbers;
Redim numbers[Ubound(strings)];
//populate array
Numbervar i;
for i := 1 to Ubound(strings) do (
numbers[i] := ToNumber(strings[i])
);
//return
numbers;
split is the correct function. i think it will probably be easiest to keep them as strings and then convert when you need to use them (otherwise you will simply have to loop through the string array and populate a new number array).
what trouble are you having with split? and what do you then intend to do with your array?

how to add leading zero to a text field in crystal reports

I'm trying to think of a way to add a leading zero to a string field value.
For example I have 12345 but need a formula that would convert it to 012345. I'm new to Crystal as I know that this is probably a simple formula but cant seem to get it to work.
12345 => 012345 (add leading zero to make it 6 chars)
Thanks in advance.
Try this
totext(your_number, "000000");
1st arg.: Well, it's the input.
2nd arg.: Number of digits you need in the output.
For ex,
num = 1234;
totext(num, "000000");
Output:
001234
And, if you want to add a fixed number of zeroes, then you can just use (to add 3 leading zeroes):
"000" + totext(your_number, 0); // to add 3 leading zeroes
Note: The final output is a string not a number.
To pad a numeric string value with zeroes to a certain length:
local numbervar yournum := tonumber({table.your_string}); //convert to number
totext(yournumnum, '000000') //convert back to padded string of length 6
OR for a universal string
local stringvar yourstring:= {table.your_string};
local numbervar LENGTH := 10; //The desired padded string length
if length(yourstring) >= LENGTH then yourstring else
replicatestring('0',LENGTH-length(yourstring)) + yourstring
how to use the formula in your crystal report,
to put below mention formula in ( open (field explorer)-->then right click(Formula field)-->select new--->create formula name(xyz)-->then put
** ToText({DataTable1.Sr_No},"0000") **
copy/past only bold text between ** and then save and close
after that formula you add to your crystal report and run, your required answer is there,
Sr_No.
0001
0002
0003
just put on your field '0'+yourstringchar
Above the logic works and its a generic formula to left padding
if you want right padding use this
local stringvar yourstring:= {table.your_string};
local numbervar LENGTH := 10; //The desired padded string length
if length(yourstring) >= LENGTH
then yourstring
else
yourstring + replicatestring('0',LENGTH-length(yourstring))
--kanthi
Right("00000000"&ToText({Table.correla}),8)
I use this:
replace((space((20)-len({Table.Field}))+({Table.Field}))," ","0")
Basically it makes leading spaces, but then replaces those spaces with zeros. By subtracting the length of the field from the added spaces, the field will always be the number of characters specified in space((XX)...
It looks overly-complex, but it actually simplifies reports that require numerous fixed-length fields. As I develop the report, I can copy-paste this code to a new formula, change the number of spaces to match the required field length, then change my field name.

crystal reports error in formula

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.