Results 1 to 5 of 5

Thread: Passing parameters to Crystal Report w/o Report Viewer

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    42

    Passing parameters to Crystal Report w/o Report Viewer

    Can anybody help me figure out how to pass a parameter to a Crystal Report in VB.NET without using a viewer? I know it would be easier with the viewer but I need to get around using the report viewer object. Thanks!

  2. #2
    Lively Member
    Join Date
    Apr 2006
    Posts
    68

    Re: Passing parameters to Crystal Report w/o Report Viewer

    G'd evening!!
    You don't need the viewer to print your rpt with/out params.
    you can do something like this:

    Code:
    Private sub PritRpt()
     Dim strPath As String = "c:\YourReport.rpt"
    
      Try
                rpt = New ReportDocument
                With rpt
                    .Load(strPath)
                    .SetParameterValue("@YourParam", CReport.intParam)
                    .PrintOptions.PrinterName = "PrinterName"
                    .PrintToPrinter(1, False, 0, 0)
                End with
       Catch ex As EngineException
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
       End Try
     End sub
    Good luck
    Estuardo

  3. #3
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Passing parameters to Crystal Report w/o Report Viewer

    Estuardo,

    This seems pretty straight forward and easy to understand, however, what I am trying to do is provide a Command Line parameter to a VB.Net application. This parameter will in turn be passed to my Crystal Report. Basically, what I am trying to accomplish is to suppress detail line printing and print sub-totals/summary lines instead based on the parameter that is passed to the report.

    I've never used parameters in CR before so I'm not even sure I've set it up right. I've basically created a Parameter Field within my Report. In the section expert, I've clicked on the "Detail" section and checked it. In the Formula Editor...all I've done was check the parameter by enter the following line:

    {?ReportType} = "D"

    What is the "CReport.intParam". I didn't see CReport defined anywhere. Is this the actual parameter value passed from the VB.Net application? If so, I keep getting this error when I run my app.

    "Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"

    Thanks,
    Last edited by blakemckenna; Nov 8th, 2007 at 05:05 PM.
    Blake

  4. #4
    Lively Member
    Join Date
    Apr 2006
    Posts
    68

    Re: Passing parameters to Crystal Report w/o Report Viewer

    G'd evening Blake!
    First of all Yes, you're right "CReport.intParam" it's a variable that holds the value for the parameter. thanks for mention it.
    I have no experience with command line parameters, but i think that if you're sure that the vb.net app. gets the param then i can help from there.
    I have created a form with the above code (fixed) and
    A Blank report in CR X. What i have in that report are two objects: The parameter and the Formula fields.
    for both i just right click on the respective category and selected "New"
    When you create the parameter a window pops up asking for three properties,
    The parameter name, the text to prompt and the Value type of parameter in
    that order i did the following:

    Name: @ThisParam
    Prompting Text:
    Value Type: Number

    For the formula field, i named flResult and in the formula editor (with the basic sintax selected) i wrote:

    Code:
    if {?@ThisParam} =1 then
    formula = "Value Is 1"
    else
    formula = "Value Is " & {?@ThisParam}
    end if
    Finally the fixed code

    Code:
    Imports CrystalDecisions.CrystalReports.Engine
    Imports CrystalDecisions.reportsource
    Imports CrystalDecisions.Shared
    
     Private Sub frmCallReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Call PrintRpt()
    End Sub
    
     Private Sub PrintRpt()
            Dim strPath As String = "c:\ThisReport.rpt"
            Dim rpt As New ReportDocument
    
            Try
    
                With rpt
                    .Load(strPath)
                    .SetParameterValue("@ThisParam", 300)
                    .PrintOptions.PrinterName = "pdfFactory" '<---A Real Printer Here
                    .PrintToPrinter(1, False, 0, 0)
                End With
            Catch ex As EngineException
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            End Try
    End Sub
    Please note that you must add the three CR references to your project in order to make this work.
    If the problem is with the command line arguments you may want to read this Investigating Command Lines

    Good luck
    Estuardo
    Last edited by Estuardo; Nov 8th, 2007 at 09:58 PM. Reason: add the load event

  5. #5
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Passing parameters to Crystal Report w/o Report Viewer

    Estuardo,

    I tried using your code and still got the same error message. Below is my exact code.

    Code:
        Public Function CreateSettlementReport() As Exception
            Try
                Dim crSettlement As New rptSettlement
                Dim diskOpts As DiskFileDestinationOptions = ExportOptions.CreateDiskFileDestinationOptions()
                Dim exportOpts As ExportOptions = New ExportOptions()
    
                strSettlementReport = "\Settlement" & strReportDate & ".PDF"
    
                exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
                exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
    
                diskOpts.DiskFileName = strPath & strSettlementReport
                exportOpts.ExportDestinationOptions = diskOpts
    
                With crSettlement
                    .Load(strSettlementReport)
                    .SetDataSource(DS)
                    .SetParameterValue("@ReportType", strReportType)
                    .Export(exportOpts)
                End With
    
                crSettlement = Nothing
                Return Nothing
    
            Catch ex As Exception
                strProcedure = "CreateSettlementReport()"
                strMsg = ex.Message
                WriteToErrorFile(strMsg, strProcedure, strProgram, intRecordCount)
                Return ex
            End Try
        End Function
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width