Crystal Reports query help if-then-else - crystal-reports

Basically I have a field in my table called sex, and it's a boolean. true for male, false for female. How can I display it this way instead of as 0, 1?

You want to create a formula field and add the formula field to the report instead of the sex field. Something like this should work (my syntax may be slightly off)
if {MyTable.sex} = 1
then
"male"
else
"female"
If you are unfamiliar with formula fields they are just expressions you can use and display in the report. In the 'Field Explorer' side bar in Crystal Reports you can see list of all the Database Fields etc. Just right click on the Formula Fields and add a new one. After you create the formula field you drag it onto your report layout just like any other normal field.

Use a formula field. Put, say, #Sex into your report, then edit it to be something like:
//may need tweaked....
If {database.table.sex} then
male
else
female;
My Crystal formula syntax is really rusty, so that may not be right as written. It might need to be :
//may need tweaked....
Stringvar displaySex;
If {database.table.sex} then
displaySex = "male"
else
displaySex = "female";
displaySex
And the line-end (";") format might not be right....

If you right-click on the original database field and choose "Format Field...", you can set the Display String on the Common tab using one of the formulas above, instead of creating a separate formula to do the calculation.

Related

How to do conditional sum in Crystal Report

Question originally posted in Spanish, on es.stackoverflow.com, by Sebastián Miranda:
I am working with Crystal Report since Visual Studio 2017, I was asked
to make a sum to group, I have 3 options: Credit Notes, Ballot and
Invoice.
The first thing I did was to create a Formula Field with the name
NumberValorCif to convert the ICIF Value that I have to numeric with ToNumber (). I saved this and created another formula field
named SumCIFCredito that has the following code:
If {CobroPorcentaje.TipoDoc} = 'Credit Notes'
then {#NumberValorCif}
else 0
I saved, compiled and ran the application and it worked without any
problems ... but. I wanted to do the same for SumCIFBoleta:
If {CobroPorcentaje.TipoDoc} = 'Ballot'
then {#NumberValorCif}
else 0
But I don't add anything. I'm missing something? or I have to create
another variable.
I hope someone can help me.
Thank you so much
It sounds like you simply need to add the actual SUM of that new formula.
Place the new formula in the detail section, select it, hit the Sigma button to sum it, and elect to generate subtotals for your Grouping level.

Create a formula

I placed my field ParentDied from table tblSIF onto my page report. If the report loads, the condition should be:
IF ParentDied = 0 THEN 'None' ELSE 'Deceased'
I searched on the Internet and found this:
`IF {#Ship Days} = 0 THEN "Same Day" ELSE {#Ship Days}`
Where did #Ship come from? Is that a parameter? All I know is that the field is named by CR as tblSIF.ParentDied. How can I make a formula of my own?
To create and use a formula on your report follow these steps:
Right Click Formula Fields and select New...
Input a name for your formula
In the Formula Workshop add in your expression
Based on your example this will be something like:
If {tblSIF.ParentDied} = 0 Then "None" Else "Deceased"
Drag the report onto the report to display
Design:
Preview:

Return different datatypes crystal formula

I have a requirement where depending on the parameter selected the table field should be displayed as text or date. For eg, if the user selects a true, the OriginDate field which is a datetime type should be displayed as a date in the report else should be displayed as a text. I tried creating a formula an doing something like below, and then placing the formula in the details section of the report but it doesnt work due to different datatypes returned.
if {?Mailmerge}=true then
ToText({Travel.OriginDatetime}, "M/d/yy")
else
{Travel.OriginDatetime}
Is there a way I can accompalish the above requirement so that I do not end up creating 2 reports one with field displayed as text and other as date?
Try creating a formula that returns your field as a string totext({Travel.OriginDateTime},"M/d/yy") and overlay it with the original field {Travel.OriginDateTime} and conditionally suppress them based on the value of {?MailMerge} such that only one shows in any one instance of the report.
If I'm following you, you want to only show M/d/yy when ?MailMerge is true, otherwise yuo want to show the full date?
You can't show different datatypes in the same formula, so just convert them both parts of the conditional statement into strings:
if {?Mailmerge}=true then
ToText({Travel.OriginDatetime}, "M/d/yy") // 8/30/12
else
ToText({Travel.OriginDatetime}, "M/d/yy hh:mm:ss") // 8/30/12 02:06:00
I don't know what exactly your dates look like in the database, but you can use the above as a guideline into formatting your date based on the ?MailMerge parameter.

CRYSTAL XI Field Editor

What formula do I use to change the field format for Age so that it looks like (String) data?
Here are some choices:
Put your field on the report. Format your field and under Display String, use cstr(currentfieldvalue). (FYI: the currentfieldvalue is actually a function).
Make a new formula. Use cstr({tablename}.{fieldname}).

'This field name is not known' error (Crystal Reports)

Here is a strange problem I have run into.
My record select formula is as follows, I have dumbed it down for purposes of this error:
reportDocument.RecordSelectionFormula = " {#ClientName} = 'Smith' "
If I copy this exact selection statement into Crystal it previews fine, but when run from .NET I get
'This field name is not known'.
The problem is occurring at the #ClientName formula field.
#ClientName simply contains:
formula = {aw_illust.CL1LNAM}
I can also set it to just a blank string (formula = "") and it still gives
'This field name is not known.'
Here is the strange part, if I bypass the formula and put the formula text straight into .NET everything works fine.
reportDocument.RecordSelectionFormula = " {aw_illust.CL1LNAM} = 'Smith' "
That is the same record selection, except with the '#ClientName' formula replaced with '{aw_illust.CL1LNAM}'. It just happens on this one report, I have many other Crystal Reports working with formulas referenced in .NET just fine. Any ideas?
Most likely you are not using your {#ClientName} formula anywhere on report; Crystal Reports tends to ignore unused fields, formulas etc. Place {#ClientName} field somewhere in report (header or some similar section) and suppress it - this way you don't mess up report design, but CR should know about that formula afterwards.
Which syntax has the formula? If it is crystal syntax then change it to basic syntax. Or change the content of the formula to crystal syntax (without "formula =")
You could create a parameter like {?ClientName} and set that from .NET, and then put a select formula in Crystal like {#ClientName} = {?ClientName}.
Can you save the report with the selection criteria you'd like then display what the reportDocument class sees using something like:
TextBox1.text = reportDocument.RecordSelectionFormula
This would confirm that your syntax is correct.
Same happened with me when I copied existing report to be used as the new one, You can first open report in report viewer ,In my case
but the query that loading report has no selection with name DateLocal Transaction so I got this error, the resolve is to either use only those that are require (Extras has nothing to do with the error) or rename selection value in query.