crystal report formula, loop on some data to write them in a single string value - crystal-reports

I have a crystal report, that returns data from a query like follow:
Quantity type
1 cat
2 dogs
5 birds...
I want to make a formula to show them in a single string value like:
1 cat; 2 dogs; 5 birds;
So How to write this formula

stringvar A;
A := A & totext({Quantity}, "#") & " " & {type} &";";
A

Related

How to add blank lines to complete an Access report so that it matches a printed form?

I deal with a bunch of government forms, and I constantly find myself trying to get Access 2013 to output a report that matches a pre-printed form.
Most of the forms are now in .pdf form, so Access doesn't handle them well (to my knowledge) without additional software, which I cannot install on user production computers.
So I usually re-create the form as an Access report, but I have real difficulty when I have only enough records for a half a page.
How do I get a report to print the required records, and then fill the page with blank records so the "form" looks correct?
I'd be willing to try any possible solution - I've even gone so far as to create blank records to try to make this work. My goal, however, is to automate this process so any user can generate the report and it prints out correctly without a bunch of fiddling.
On this form, some or all of the lines might be used, and each person (I have ~550 people who each have an individual form) has a different number of lines, depending on the number of jumps they have completed.
I would have a dummy table with just a single numeric field called id. Populate this with a number of records greater than the biggest number of 'extra' records you're ever going to need to fill your form, using numbers from 1 upwards for id. Now say your original record source query for the report is:
select field1, field2, field3, field4
from myTable
order by field1
and you always want 15 rows to fill your form, then change the report's record source to:
select top 15 sort_seq, field1, field2, field3, field4
from (
select 0 as sort_seq, field1, field2, field3, field4
from myTable
union
select id, null, null, null, null
from dummyTable
) as X
order by sort_seq, field1
Ok, so you are recreating the complete PDF form as Access report. And the lower part is the details section of a continuous report.
The easiest way is indeed to create the correct number of empty records, but do this automatically (e.g. using DAO Recordset.AddNew).
I like to use a separate dummy table for this (with the same structure as the original table) to not pollute the original table with dummy records.
The form has 20 lines, so if your original table has 7 records for the current person, insert 13 records into the dummy table, using a large PK so they get sorted to the bottom.
Then use a query as record source for the report, joining the master table with a UNION of Jumps and Dummy table.
Alternatively there is the Report.Line() method: https://msdn.microsoft.com/en-us/library/office/ff198297.aspx
But this will be a lot more complicated to get it right.
I hope you have found the solution to this issue. I am posting this in the hope that if someone else finds this same question they will find this one as the best possible solution, as I have been struggling for about 2 days now.
I suppose you have at least two tables and a query where you mix information from both, and your report is made out from this query. I will call this your Query1, so the trick is that when you open the Report from your Form, you will use the label number of your Form to make the filter of the Query1 from where you will get the information for your report. Like this:
rectotal = DCount("*", "Quote-Report", "[Quote_No]=" & stQtNo)
This instruction will count how many records you have on your “Quote-Report” Query that are of the same value of the [Quote_No] field inside your Quote Form , as you are assigning the stQtNo variable to the Form with the name “Quote”; and put this value on your rectotal variable.
Now with rectotal you need to determine how many lines you want in your report. I used 28 so the following formula will give how many blank rows needed to fill the page.
If rectotal / 28 - Int(rectotal / 28) = 0 Then
x = 28
Else
x = 28 * (rectotal / 28 - Int(rectotal / 28))
End If
blanklines = 28 - x
tLines = rectotal + blanklines
Of course you can use 15, 20 or as many rows you want your report to print per page. The variable blanklines will give you this number.
Then I use the recordset procedure from Access to open the table where I have the information that will print in the detail section of the report and fill with as many records as I need using the blanklines variable and the Do Until Loop.
Set db = CurrentDb
Set rs = db.OpenRecordset("Item_QD", dbOpenDynaset, dbseechanges)
Item_QD is the table
where I want to insert new records to fill the report detail section.
x = 0
rs.MoveLast
Do Until x = blanklines
rs.AddNew
rs![Quote_No] = stQtNo
rs![CODE No] = "Z" & x I use the Z and the number so I can erase the records once I don’t need them anymore.
x = x + 1
rs.Update
Loop
Please note that you need to Dim db As Database and Dim rs As Recordset variables outside your private sub in module header. Also your Access needs to be running these two classes so you can use them.
Finally once you open your report you have to go to your detail section and do a requery command to refresh all the rows prior to print. Need to format your detail cells in order to print the grid line and the rest is history.
The beauty of this procedure is that you can add and handle as many rows as you want or need inside your report and make a professional look of your quotes, packing slips, purchase orders, etc.
Hope this solves many problems. For a fast reference on how to use and manage ACCESS record set visit: https://www.youtube.com/watch?v=WNm17l54z1c
Private Sub Report_Open(Cancel As Integer)
Dim blanklines, tLines As Integer
Dim rectotal, x As Long
Dim stQtNo As String
Set db = CurrentDb
Set rs = db.OpenRecordset("Item_QD", dbOpenDynaset, dbseechanges)
stQtNo = Forms![Quote].[Quote_No]
rectotal = DCount("*", "Quote-Report", "[Quote_No]=" & stQtNo)
If rectotal / 28 - Int(rectotal / 28) = 0 Then
x = 28
Else
x = 28 * (rectotal / 28 - Int(rectotal / 28))
End If
blanklines = 28 - x
tLines = rectotal + blanklines
x = 0
rs.MoveLast
Do Until x = blanklines
rs.AddNew
rs![Quote_No] = stQtNo
rs![CODE No] = "Z" & x
x = x + 1
rs.Update
Loop
End Sub
Another solution. Code behind report. I use it for a simple report without grouping and row height doesn't change.
Option Compare Database
Option Explicit
Public totalCount As Integer
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
printBlankRecords getnumberofBlanks
End Sub
Public Sub printBlankRecords(numberBlanks As Integer)
Dim recordCount As Integer
recordCount = DCount("*", Me.RecordSource)
totalCount = totalCount + 1
If totalCount = recordCount Then
Me.NextRecord = False
'once you get to the last record, stay on last record
ElseIf totalCount > recordCount And totalCount < (recordCount + numberBlanks) Then
Me.NextRecord = False
'make the font and backcolor the same appearing to be empty record for each field
Me.RateID.ForeColor = Me.RateID.BackColor
End If
End Sub
Public Function getnumberofBlanks() As Integer
Dim recordCount As Integer
Dim N As Integer, X As Integer
X = 20
recordCount = DCount("*", Me.RecordSource)
If recordCount <= X Then
getnumberofBlanks = X - recordCount
Else
N = 1
Do While N * 21 + X <= recordCount
N = N + 1
Loop
getnumberofBlanks = (N * 21 + X) - recordCount
End If
End Function

SAS collapse data by identifiers

I have a bunch of data that I would like to collapse by a few identifier variables and keep only the non-missing values of other variables. For each unique combination of id, title, info there is 1 value of var1/var2/var3 that isn't missing that I would like to keep. Note that var3 is numeric while var1/var2 are character.
I have data like:
id title info var1 var2 var3
1 foo Some string here string 1
1 foo Some string here string 2
1 foo Some string here number 3
2 bar A different string string 4 string 5
2 bar A different string number 6
3 baz Something else string 7 number 8
And I want it to be like:
id title info var1 var2 var3
1 foo Some string here string 1 string 2 number 3
2 bar A different string string 4 string 5 number 6
3 baz Something else string 7 number 8
Thanks!
The UPDATE statement can handle that. The last non-missing value will be used. The UPDATE statement takes exactly two datasets, the original and the transaction. The original dataset must have only one observation per by group. But you can use your single dataset by using the OBS=0 dataset option to create an empty master dataset.
First here is your sample data.
data have ;
infile cards dsd truncover ;
length id 8 title info var1-var3 $20 ;
input id -- var3 ;
cards;
1,foo,Some string here,string 1,,
1,foo,Some string here,,string 2,
1,foo,Some string here,,,number 3
2,bar,A different string,string 4,string 5,
2,bar,A different string,,,number 6
3,baz,Something else,string 7,,number 8
;;;;
Here is the step to collapse.
data want ;
update have(obs=0) have ;
by id title info;
run;

Crystal Report, Grouping Rows into single Row on the basis of Criteria

I have a crystal report, which shows different fruits data on the basis of Fruit Name Group.
I am attaching a screen shot which tells the clear requirement, I want to group same type of fruits merge all into one rows and sum of their values as well.
I need help, can someone suggest a better idea?
1) Add a Formula Field
This formula field, should return
- real FruitName for the cases you don't want to group,
- a fake FruitName for the rows you want to group together.
Something like this (drag'n drop fields into Formula editor):
if {commandname.FruitName} <> 'Orange' AND FruitName <> 'Lemon'
AND FruitName <> 'Grape Friut' then {commandname.FruitName}
else 'Orange, Lemon, Grape Friut'
2) use Group Expert to create a Group based on the new Formula Field
3) suppress details and show in Group Header or Group Footer Totals for each field
I am not able to understand completely you problem.. but try below solution: If it is useful use it else discard
Concatenate Fruit Names: Create one formula #Fruit
Local StringVar orange;
Local StringVar Lemon;
Local StringVar Grape;
if <<databasefield.fruitname>>="Orange"
then orange:=<<databasefield.fruitname>>
else if <<databasefield.fruitname>>="Lemon"
then Lemon:=<<databasefield.fruitname>>
else if <<databasefield.fruitname>>="Grape"
then Grape:=<<databasefield.fruitname>>;
orange+Lemon+Grape;
Summing the values: Create one formula #Weight
Local NumberVar orange;
Local NumberVar Lemon;
Local NumberVar Grape;
if <<databasefield.fruitname>>="Orange"
then orange:=<<databasefield.Weight>>
else if <<databasefield.fruitname>>="Lemon"
then Lemon:=<<databasefield.Weight>>
else if <<databasefield.fruitname>>="Grape"
then Grape:=<<databasefield.Weight>>;
orange+Lemon+Grape;
Edit:------------------------------------------------------------------------------------
you need to have grouping something like below.
Fruit name Fruit Type
Orange a
Grape a
peach b
apples c
now when you group by fruit type and apply the formula I have provided then you can get desired results
I have done with other way, calculated these fruits values as a separate row, sum up all the values and then applying union , In union I have concatenated the fruit names using COALESCE function

crystal reports formula field select row with minimum value

i have following data :
Name Amount
================
Amit 18000
Ajay 19500
Bharat 16100
how do i print the name with lowest amount in the report footer.
i tried the following formula
if {table.amount} = minimum({table.amount}) then
'Lowest Vendor - ' + {table.name}
above formula returns a blank value.
I need this in Crystal Reports not in RDBMS.
Take a local variable and store the value in that and use the variable in report footer.
Shared Stringvar a;
Shared Stringvar b;
a:=
if {table.amount} = minimum({table.amount}) then
'Lowest Vendor - ' + {table.name}
else '0';
if a<>'0'
then b:=a;
place this formula beside the amount column.
Create another formula and write below formula and place it in footer
Shared Stringvar b;
b

Crystal Reports: multi-value parameter to filter on substring value

I have a Crystal Reports 2008 file with a string parameter that accepts multiple values. I need to use this in the record selection. I know generally you can do something like
{MyTable.MyField} In Join( {?MyParam}, "," )
but I need users to enter values that may appear in a much longer field value, i.e., by substring. I tried
NumberVar index;
For index := 1 To UBound( {?MyParam} ) Do (
{?MyParam}[index] In {MyTable.MyField}
)
and while it doesn't throw an error, it doesn't seem to have any effect on record selection (that is, the report displays the same number of records regardless).
To be more specific, say MyTable has three records, and MyField contains the text Red Blue Green, Green Yellow Purple and Red Yellow Orange respectively. With the parameter, the user should be able to type the values red and blue in order to filter down to the first and third records.
Create a custom formula to compare two arrays for over-lapping values:
//Array_Overlap
Function (Stringvar Array a0, Stringvar Array a1)
Local Numbervar i;
Local Numbervar j;
Local Booleanvar found:=false;
For i := 1 To Ubound(a0) Do (
For j := 1 To Ubound(a1) Do (
If a0[i]=a1[j] Then (
found:=true;
Exit For;
)
)
);
found;
Reference it in your record-selection formula (RSF):
// space delimited
AND Array_Overlap( Split({MyTable.MyField}, " "), {?MyParam} ) = TRUE