Passing Date Parameters to Crystal Report in PHP

sql

#1

I know, this question has been asked many times and answered as well, but still can't get it working while supplying parameters values, though without passing the parameter values, my Report is working fine with both PDF and Excel Export, please help me to fix this.

I've searched everywhere to get the right path to follow but no luck, below is my code.

I've tried this, but I'm getting error http://www.sitepoint.com/forums/showthread.php?486356-Inserting-date-parameters-into-a-Crystal-report

<?php 

//- Variables - for your RPT and PDF 
echo "Print Report Test";
$my_report = "C:\\AppServ\\www\\Acquiring\\reports\\rpt-group.rpt"; // rpt source file 
$my_pdf = "C:\\AppServ\\www\\Acquiring\\reports\\TempFiles\\rpt-group.pdf"; // RPT export to pdf file 
//-Create new COM object-depends on your Crystal Report version 
$ObjectFactory= new COM("CrystalReports115.ObjectFactory.1") or die ("Error on load"); // call COM port 
$crapp = $ObjectFactory-> CreateObject("CrystalDesignRunTime.Application"); // create an instance for Crystal 
$creport = $crapp->OpenReport($my_report, 1); // call rpt report 

//- Set database logon info - must have 
$creport->Database->Tables(1)->SetLogOnInfo("SystemName", "DBName", "SQLServUser", "SQLServPWD"); 

//- field prompt or else report will hang - to get through 
$creport->EnableParameterPrompting = 0; 

//- DiscardSavedData - to refresh then read records 
$creport->DiscardSavedData; 
$creport->ReadRecords(); 

//—— Pass formula fields
//$creport->FormulaFields->Item(0)->Text = ("My Report Title");
$creport->ParameterFields(1)->AddCurrentValue("2016-08-26"); //CANNOT GET IT WORKING
$creport->ParameterFields(2)->AddCurrentValue("2017-08-26"); //CANNOT GET IT WORKING

//- export to PDF process 
$creport->ExportOptions->DiskFileName=$my_pdf; //export to pdf 
$creport->ExportOptions->PDFExportAllPages=true; 
$creport->ExportOptions->DestinationType=1; // export to file 
$creport->ExportOptions->FormatType=31; // PDF type | 29 Excel Type
$creport->Export(false); 

//------ Release the variables ------ 
$creport = null; 
$crapp = null; 
$ObjectFactory = null; 

//------And Now -> Embed the report in the webpage ------ 
print "<embed src='http://localhost:82/acquiring/reports/TempFiles/rpt-group.pdf' width=\"100%\" height=\"100%\">"
?>

#2

I've never tried this, but did a quick google and found this page: https://stackoverflow.com/questions/11862458/pass-date-parameter-from-php-to-crystal-report

It suggests that perhaps calling a vbscript function to convert the date format is the way forward. Seems a bit of a hack (as the poster of that solution commented) and I'm struggling to think where the VBScript would run, and whether that would have dependencies on server software and the like.


#3

Thank you for your effort, I've already tried this solution, and when applying this hack, it always gives an error, that the $rptParams is Null and Unable to move forwards due to NULL values, I've tried passing Parameters but no luck.

$rptParams = $report.ParameterFields;
$rptParam = $rptParams->Item(2);

// This bit should be fine
$oScript = new COM("MSScriptControl.ScriptControl");
$oScript->Language = "VBScript";
$oScript->AllowUI = false;
$oScript->AddObject('rptParam', $rptParam, true);
$oScript->AddCode('Function SetDateParameter(strDate)
rptParam.AddCurrentValue(CDate(strDate))
End Function');
$oScript->Run("SetDateParameter", "25 April 2006");


#4

I imagined you would already have found that, and that you've changed it to $creport.


#5

Yes @droopsnoot (Thanks for the Clue) I've already done that, but not able to apply multiple parameter values:

$rptParams = $creport->ParameterFields;
$rptParam = $rptParams->Item(2);

// This bit should be fine
$oScript = new COM("MSScriptControl.ScriptControl");
$oScript->Language = "VBScript";
$oScript->AllowUI = false;
$oScript->AddObject('rptParam', $rptParam, true);
$oScript->AddCode('Function SetDateParameter(strDate)
rptParam.AddCurrentValue(CDate(strDate))
End Function');
$oScript->Run("SetDateParameter", "25 April 2006");


closed #6

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.