Print Page | Close Window

Add date shared variable as parameter

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=6347
Printed Date: 01 May 2024 at 2:02pm


Topic: Add date shared variable as parameter
Posted By: pto160
Subject: Add date shared variable as parameter
Date Posted: 13 May 2009 at 11:54am
I have a date value shared variable. I have an inventory forecasting report that calculates an out of stock date in Crystal Reports X1. The parameter would ask you the date. I would put in 6/1/09. The report would show only dates equal to or less than this date on the report. Is there a way to do this?



Replies:
Posted By: DBlank
Date Posted: 13 May 2009 at 8:07pm
If you are asking how to create a user entered parameter that can be used in a select statement...
Right Click on Parameters and select New.
Enter a parameter name, we'll call it "Date" here.
Choose the field data type of Date.
Save and close it.
In the Select Expert enter your formula using your new parameter:
{table.datefield}<={?Date}


Posted By: pto160
Date Posted: 14 May 2009 at 8:24am
Thanks. The out of stock date shared variable is not a field in the database. It is a calculated formula.
Will this method still work or do I have to somehow share it with the main report?



Posted By: DBlank
Date Posted: 14 May 2009 at 8:43am
Depends on how and where it is calculated.
What is the formula?


Posted By: pto160
Date Posted: 14 May 2009 at 8:54am
The formula is in a sub report called Shipped Qty. The formula is as follows:

DateVar ReturnDate;
if {@Monthly Forecast}=-1 then ReturnDate:= Date (2099,12 ,31 );
if {@Monthly Forecast} <> -1 then
(Shared dateTimeVar Shared_Out_of_Stock_Date :=CurrentDateTime + {@Monthly Forecast} ;
ReturnDate:=Date( CurrentDateTime + {@Monthly Forecast}));
ReturnDate

The Shared_Out_of_Stock_Date is the variable. I need to get this as the parameter. 


Posted By: DBlank
Date Posted: 14 May 2009 at 9:07am
Variables are not my forte but lockwelle will correct me if I am wrong here...
It appears you are not using this parameter date in the main report, only the sub report.
You can create the parameter in the main report without placing it in your select statement.
When you create your "Shared_Out_of_Stock_Date variable" jsut set it to  = {?date} parameter.
 
If this does not set it to ask for the parameter at run time just drop the {?Date} parameter field in your main report report header and suppress it.
 


Posted By: pto160
Date Posted: 14 May 2009 at 11:39am
I will try that and see what happens. 


Posted By: lockwelle
Date Posted: 15 May 2009 at 11:06am
Sorry, I don't usually look at items that DBlank has answered as they correct and there is nothing for me to add...had some time and the title intrigued me.
 
I am a bit confused by how things are being used.  I understand that you are entering a date and you want all the items that will be projected to be out of stock on or before that date.  I think I have that right.
 
You have a formula in a subreport that does a calculation, but that is not real clear.  Is this what you were getting at:
local DateVar ReturnDate := Date (2099,12 ,31 );
Shared dateTimeVar Shared_Out_of_Stock_Date := ReturnDate;
if mailto:%7b@Monthly - {@Monthly Forecast} <> -1 then(
 Shared_Out_of_Stock_Date := CurrentDateTime + mailto:%7b@Monthly - {@Monthly Forecast} ;
 ReturnDate:=Date( CurrentDateTime + mailto:%7b@Monthly - {@Monthly Forecast}));
);
ReturnDate
Either way, you have this shared variable Shared_Out_of_Stock_Date being set in the subreport.  Here comes the big question...can you see this subreport?  Does the subreport do more than this, and if this item is to be 'in stock' should it be displayed?
 
why do I ask?  You are going to want to suppress something (and possibly this subreport) in the main report based on the variable Shared_Out_of_Stock_Date, and if you need to run the subreport before you can suppress the subreport, you are in a catch-22.  If the subreport is already suppressed, then life might be easier, depending on how the report is displayed, but as DBlank said, it doesn't appear that you are looking at the variable, you created it and set a value, but now what...
 
In the main report, create another formula:
shared datevar Shared_Out_of_Stock_Date;
 
this will display the results of the shared variable.  you can use it in suppression formula for a row, a field, or whatever you want to do with it.  Just remember that Crystal basically reads a report from top to bottom stopping to read subreports, so if the subreport is after the line you want to hide, you will have problems.
 
As always, if you got the data via a stored proc, you wouldn't need this as you could suppress the records from every making it to your report and the need of a subreport would go away...they are impose a lot more data reads to the database...1 is enough in the vast majority of instances...but the number of rows + 1 can really the impede the performance of your report.
 
Hope this made sense.


Posted By: pto160
Date Posted: 15 May 2009 at 11:53am
Thanks. So if I add the shared variable to the main report, I can then create s user input parameter? The shared variable is displayed on the subreport.
I will try this and see if that works.


Posted By: lockwelle
Date Posted: 15 May 2009 at 1:09pm
you would have already wanted to have a user parameter.  The parameter is available through the report.  Yes, the shared variables are available in both the main report and the subreport...global variables do not cross the boundary between shared and main.


Posted By: pto160
Date Posted: 15 May 2009 at 5:51pm
I followed your advice and added the values from the shared variable to the main report and it is displaying correctly. I added a section below the subreport. How do I add the shared variable in the main report as a parameter? Do parameters only work with fields in the database? If I can get this, this will save tonnes of paper.


Posted By: lockwelle
Date Posted: 18 May 2009 at 6:43am
I'm not sure, as I no longer use the parameters in the reports, I just pass the desired dataset to the report to display.
 
You should be able to set the shared variable to a parameter.  I would try a formula that assigned the parameter to the shared variable and place this new formula in the report header.  something like:
 
shared numbervar whatIwant := @parameter;
""   //to hide the assignment value


Posted By: DBlank
Date Posted: 20 May 2009 at 6:08pm
Debmac posted this nice link for another question that should resolve your issue on using one parameter in the main report for all of your sub reports...
http://www.tek-tips.com/faqs.cfm?fid=1329


Posted By: pto160
Date Posted: 21 May 2009 at 7:13am
Thanks. The shared date variable is in the subreport (Called shipped qty) because it is pulling most of the information from other formulas in the subreport that use the Sales table. It is pulling the net stock from the main report. It is not a table date field in the database.

Is there a way to pass the shared date variable as a record selection based on a parameter even though it is not a field in the database?
I have been told that I may need to use conditional suppressing to make this work.
I can use rapidshare.com to send the file if need be.

 


Posted By: lockwelle
Date Posted: 21 May 2009 at 7:32am
I'm sure what you mean by passing a shared date variable as a record selection.
 
shared is shared, it has access to all parts of the report.  if you want to use it to select records for a stored proc or subreport, then I would create a formula that just 'displays' the shared variable and then use it in the linking of the subreport to the parameter that needs it.
 
I don't know if this work, but it is what I would try first...this is setting the parameter of the subreport to the value that you want.  I can't see why this wouldn't work, but Crystal has quirks, and I just might not have come across this one, yet.


Posted By: pto160
Date Posted: 21 May 2009 at 12:57pm
Thanks. Would I create the formula that just 'displays' the shared variable in  the subreport or the main report?



Posted By: lockwelle
Date Posted: 21 May 2009 at 1:13pm
in the main report so that you can use it to link to the parameter in the subreport.


Posted By: pto160
Date Posted: 21 May 2009 at 3:56pm
And in the subreport I would put
shared numbervar whatIwant := @parameter;
""   //to hide the assignment value.
 and then use subreport links to link the main report and the subreport?



Posted By: lockwelle
Date Posted: 22 May 2009 at 6:24am
I don't think so.  the subreport is gathering some data from the database.  It is usually gathering this information based on some criteria.  I thought that a part of the criteria was the value in the shared variable.  Either way, the subreport is 'Linked' to the main report via the parameters.  Right click on the subreport from the main report and choose 'Change Links' or something like that.  I would attempt to link the formula in the main report to the appropriate parameter in the subreport.
 

Does this make sense?
 


Posted By: pto160
Date Posted: 22 May 2009 at 8:08am
Thanks. Yes, I am basing it on a shared variable in the subreport.
Okay to summarize. I displayed the shared variable in the main report using a shared variable called Return Date (Out of stock date), which is displaying the correct date. I created a parameter in the subreport called Out_Of_Stock_Date and linked it to the Return Date formula in the main report using change subreport links.
So, how would I create the parameter so the user puts in the Out of Stock Date? I take it I need to create a parameter called Out_of_Stock_Date in the main report?



Posted By: lockwelle
Date Posted: 26 May 2009 at 6:05am
Sounds right. 
For some reason I thought that the out of stock parameter was coming from the data.  If it is something that the use can enter via a parameter, you don't need the shared variable.  You can just link the subreport to the main reports parameter Out_Of_Stock_Date.
 
Sorry to lead you astray, again, I thought that the date was going to be generated from the data, not a parameter.


Posted By: pto160
Date Posted: 26 May 2009 at 4:34pm
Thanks. In the subreport I tried to create the parameter by using the following formula.

Shared DateVar ReturnDate;
ReturnDate<={?Out of Stock Date}
This turn it into a boolean.

I tried
Shared DateVar ReturnDate;
ReturnDate:<={?Out of Stock Date}.

It says a boolean is required here.

So I am  not sure how to write the user input parameter in the subreport?



Posted By: lockwelle
Date Posted: 27 May 2009 at 6:30am
I am not sure where you are using this, but I am guessing that what you want is:
ReturnDate := {?Out of Stock Date}
 
This will assign the value of the field to the shared variable.
 
if you want to compare for the purposes of the selecting data:
if {some data field} >= ReturnDate then
 true //or false which ever is correct
else
 false //or true
 
This can shortened to just:
{some data field} <= ReturnDate
which can be used in filter or suppression formulas, but an assignment is the first example.


Posted By: pto160
Date Posted: 27 May 2009 at 4:26pm
Do I have to use the section expert if I try to suppress the results? It may look like I have to use a sequel command or stored procedure in order to record select the results properly. Suppressing the results means that everything is returned but we are essentially hiding the results that do not match the parameter. I tried doing that but it got very messy.


Posted By: lockwelle
Date Posted: 28 May 2009 at 6:13am
if you can filter the values in the SQL selection, all the better.  Many who post, don't use stored procs or SQL, they just bind to the tables.
 
Given that you are going to use a SQL expression, you might not need to use the suppression, but it will all depend on what data is returned and what you want displayed.


Posted By: pto160
Date Posted: 28 May 2009 at 4:19pm
Thanks. I sat down with a Crystal Reports developer for 4 hours a year or two ago and we were using the section expert to suppress the results that did not match the parameter. The problem was that it would return only the correct results for about a page but would not return anything after that.
Then we started to add a copy of the subreport section and base the suppression on this subreport copy. Still did not work. I have not seen anything on this in any book or the business objects support section of their website.
Can shared variables be calculated using a stored procedure on the database? I have been told that shared variables are only evaluated "while printing records".


Posted By: lockwelle
Date Posted: 29 May 2009 at 6:07am
I was taught by a Crystal Report tech support person...way long ago.  If you want to suppress records that don't match the parameters, filter them out in the Report/Select Formulas/Records.
 
shared variables are calculated by the report.  If you want to suppress something in a stored proc, just don't return it.  You can use a stored proc to return multiple tables and then use the value for one of the tables as a shared variable, but why?  Just use the value.
 
I do this in every report.  My stored proc returns 2 tables (sometimes more, but usually 2). 1 is the data for the report, 1 is the parameters used to generate the report (my data comes from an XML file...ok, ado.net really).  I will use the parameter information to, in some cases, to 'dynamically' structure/group my report.
 
As for suppressing a section, like a row of data or a footer, etc, if it is not always visible or always suppressed, then you need to use the Section Expert to enter the formula that will suppress it.
 
Long winded, but hopefully helpful to all.


Posted By: pto160
Date Posted: 04 Nov 2009 at 9:42am
I have researching this issue lately again. The best way is through a stored procedure, and maybe the only way that works properly. Can I just write the shared date variable formula as a stored procedure, and leave the rest of the report as is? 


Posted By: lockwelle
Date Posted: 04 Nov 2009 at 11:45am
the easiest solution is to gather all your data in the stored proc.  I haven't ever mixed a stored proc and linking to tables in Database Expert in the same report. I would think that you could, but again, the simplest is to use the stored proc.
 
The down-side, is when you change the datasource, it will erase your report, but this can be averted.  Add the stored proc as datasource and then replace each field as needed until the little checkmarks are all gone in the old datasource, then you can safely delete them as datasources.
 
HTH


Posted By: pto160
Date Posted: 04 Nov 2009 at 12:33pm
Thanks. Would it be necessary to have database administrator rights to execute the stored procedure? Do I have to write the stored procedure on the database, or can I use crystal reports to store and activate the stored procedure?
The problem is that the database is controlled by the ERP vendor. They currently allow us to retrieve the data from the database using crystals but I am not allowed to modify or make any changes to the database. I am a bit of a novice in how stored procedures operate. Could you recommend a book that tells you how to write stored procedures? Your help has been invaluable.


Posted By: lockwelle
Date Posted: 05 Nov 2009 at 6:11am
sorry, no help on a book...
Yes, stored procs are created and run on the database, so...
 
Crystal does have something about SQL Expressions, but I am not sure how they work, as I use the stored proc and push the data to my report instead of having the report pull the data from the database.
 
Sorry I can't be of more help.


Posted By: pto160
Date Posted: 05 Nov 2009 at 11:27am
Thanks for all your advice in this area. It looks like stored procedures is the best and probably only way to do this. At least, I know the direction I am going in and not wasting time on using formatting suppression techniques within Crystal Reports. Once again, thanks.





Print Page | Close Window