crystal formula to compare a string field and a number field - crystal-reports

I'm trying to compare two fields that contain up to 2 digit field.
One formula field is a {string} the other to compare to is a {number} field.
But i"m running to a problem when the string field shows for example "08" and the number field shows "8" then it will show 1 that there is a differences but actually there is no difference. If the string field shows 14 and the number field shows 14 that works perfectly but anything between 1-9 will show a difference when actually there is no difference.
This is what I tried so far.
If {number.field} = 0
Then StringVar AdjustValue:= " "
Else StringVar AdjustValue:= totext ({number.field},0,"")
;
if {#stringfield} = StringVar AdjustValue then 0 else 1
Thanks in advance for the help.

Instead, convert from string to numeric so you don't have to worry about leading zeros.
if tonumber({#stringfield})={number.field} then 0 else 1
And just a quick side note: You are re-declaring the AdjustValue variable three times. There's no need to refer to it via "StringVar" after an initial variable declaration within the same formula.
EDIT: Since you're having problems with the top formula, you could also try the alternative of just padding your AdjustValue variable to two spaces:
stringvar AdjustValue;
If {number.field} = 0
Then AdjustValue:= " "
Else AdjustValue:= totext ({number.field},"00") //add padding
;
if {#stringfield} = StringVar AdjustValue then 0 else 1

Keep it simple with a formula field that will return a Boolean value:
// {#compare}
ToNumber({#string})={number.field}
Reference the {#compare} where ever you need it.

Related

Display number of rows on printout

If an Order contains 2 lines or more, my report sums it up and prints the whole quantity of items. It should print whichever line I have chosen:
WhilePrintingRecords;
NumberVar ItemCount := ItemCount + 1;
ToText(ItemCount, "0") & "/"
& ToText(Count({rpt_PackingSlip.LabelQTY}, {rpt_PackingSlip.WorkOrderNo}),0,"")
For example, the order below contains a chair called Buzz, but the order contains 3 lines since each has different fabric. The total order quantity is 5:
If I print, the label count shows 1 out of 4 - which automatically sums the chair. If I select the first line, expected output is Buzz 1/2.. and 2/2. Currently output displays Buzz 1/4.. 2/4.. 3/4.. 4/4.. even if I just clicked 1st line. How can I achieve this result?
You'll want to reset the counter on each group. Just create a second formula and drop it in the group header:
global numbervar ItemCount := 0;

Crystal reports formula using variables

I need a formula like this:
if {#TarSale_TimeWise}= 0 or isnull({#TarSale_TimeWise}) then '-'
else
totext(round(({#ActSale_TimeWise}-{#TarSale_TimeWise})/{#TarSale_TimeWise}*100,1),1)
Here the formula for {#TarSale_TimeWise} is
round({CatTimeWise.tarSale},1)
Similarly the formula for {#ActSale_TimeWise} is
round({CatTimeWise.ActSale},1)
I need to take rounding as 1 for both the fields and then I need to do the calculation for var sale with values round as 1 for both the formulas.
Now instead of creating this below 2 formulas and using those in 3rd formula I want to create only one formula using variables.
How can I modify above formulas as 1 formula?
Create only one formula and write below code:
Local Numbervar a1:=round({CatTimeWise.tarSale},1) ;
Local NumberVar a2:=round({CatTimeWise.ActSale},1) ;
if a1= 0 or isnull(a1) then '-'
else
totext(round((a2-a1)/a1*100,1),1)
Try:
If Isnull({CatTimeWise.tarSale}) Or {CatTimeWise.tarSale}=0 Then
'-'
Else
ToText( Round( (({CatTimeWise.ActSale} - {CatTimeWise.tarSale}) / {CatTimeWise.tarSale}*100), 1), 1 )

Calculation of Previous field

New to CR and use CR v10 and SQL Server 2000.
For the first record i.e Beginning Balance , the calculation is sum(field) from the input date, which I have calculated in SP as BegDateSum
But for the rest of the records under a group, the calculation should be previous(balance)+IN+OUT
Sample has been given:
Date Doc Descrip IN OUT Balance
Group Header-------- Beginning Balance-------------- 50 <---- sum(field) from my inputdate
3/2/2012 A -1 0 49 <-- (50+(-1)+0)
4/2/2012 B -2 0 47 <-- (49+(-2)+0)
5/2/2012 C 0 3 50
6/2/2012 D -2 3 51
How do I achieve this?
I am not sure whether to use running total, in case I have to how to do it.
A running total field won't work in this case, they are designed to add up (or count, or average, etc) one field and give you the sub-totals automatically. But, we can do some custom functions that will give the results you need. Assuming that your initial 50 is a static value, you would set a variable to that amount, and then add the IN and OUT values as you go along (printing that result of that).
First, initialize the value in the report header with a formula like:
WhilePrintingRecords;
Global NumberVar Balance;
Balance := 50;
""; //print nothing on the screen
Then, the formula to calculate and show the new balance, in the bar where the data is:
WhilePrintingRecords;
Global NumberVar Balance;
Balance := Balance + {tableName.IN} + {tableName.OUT};
The last line both calculates the new value, and tells what the result of the formula should be.
If the "50" is calculated somehow, then that will have to be done before the formula that calculates the new balance. If it is based off of the first record read in, you'll want to use a formula that includes If PreviousIsNull({tableName.Balance}) Then ..., that is usually a good indicator of the first record in the data set (unless that field can be null).

sum two values from different datasets using lookups in report builder

I have a report that should read values from 2 dataset by Currency:
Dataset1: Production Total
Dataset2: Net Total
Ive tried to use:
Lookup(Fields!Currency_Type.Value,
Fields!Currency_Type1.Value,
Fields!Gross_Premium_Amount.Value,
"DataSet2")
This returns only the first amount from dataset 2.
I've tried Lookupset function as well but it didn't SUM the retrieved values.
Any help would be appreciated.
Thanks Jamie for the reply.
THis is what i have done and it worked perfect:
From Report Properties--> Code , write the below function:
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
ct = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
Next
If (ct = 0) Then return 0 else return suma
End Function
Then you can call the function:
code.SumLookup(LookupSet(Fields!Currency_Type.Value, Fields!Currency_Type1.Value,Fields!Gross_Premium_Amount.Value, "DataSet2"))
Yes, Lookup will only return the first matching value. Three options come to mind:
Change your query, so that you only need to get one value: use a GROUP BY and SUM(...) to combine your two rows in the query. If you are using this query other places, then make a copy and change that.
Is there some difference in the rows? Such as one is for last year and one is for this year? If so, create an artificial lookup key and lookup the two values separately:
=Lookup(Fields!Currency_Type.Value & ","
& YEAR(DATEADD(DateInterval.Year,-1,today())),
Fields!Currency_Type1.Value & ","
& Fields!Year.Value,
Fields!Gross_Premium_Amount.Value,
"DataSet2")
+
Lookup(Fields!Currency_Type.Value & ","
& YEAR(today()),
Fields!Currency_Type1.Value & ","
& Fields!Year.Value,
Fields!Gross_Premium_Amount.Value,
"DataSet2")
Use the LookupSet function as mentioned. With this you'll get a collection of the values back, and then need to add those together. The easiest way to do this is with embedded code in the report. Add this function to the report's code:
Function AddList(ByVal items As Object()) As Double
If items Is Nothing Then
Return 0
End If
Dim Total as Double
Total = 0
For Each item As Object In items
Total = Total + CDbl(item)
Next
Return Total
End Function
Now call that with:
=Code.AddList(LookupSet(Fields!Currency_Type.Value,
Fields!Currency_Type1.Value,
Fields!Gross_Premium_Amount.Value,
"DataSet2"))
(Note: this code was not tested. I just composed it in the Stack Overflow edit window & I'm no fan of VB. But it should give you a good idea of what to do.)

Crystal Reports/FormatEditor/Display String formula: How to format data

I want to specify a formula for the display string property of an object on my report, however I want to use it to format the data that would otherwise show up if I were not using this formula. Is there a keyword or anything I can reference within the formula that represents what would normally be displayed?
You might need to create a formula field and reference your database field within it:
for summarized fields, I use min(#cellVal) in the crosstab:
dim cVal as number
cval = {vw_rpt_waitlist.CountOfRecipientID}
if cval < 0 then
formula = " "
elseif cval = 0 then
formula = ""
elseif cval < 5 then
formula = "< 5"
else
formula = cstr(cval,0,",")
end if
and in the rows list, to modify the sorting:
if {vw_rpt_waitlist.SupportCode} < 100 then
formula = {vw_rpt_waitlist.description}
else
formula = "z" + totext({vw_rpt_waitlist.SupportCode}) + {vw_rpt_waitlist.description}
end if
It doesn't seem this is possible as far as I've been able to tell.