Results 1 to 18 of 18

Thread: [RESOLVED] Using Parameters w/Crystal Reports XI?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Using Parameters w/Crystal Reports XI?

    I am trying to pass a parameter to a Crystal Report from a VB.Net app. Based on the parameter value, it will either print detail lines or sub-total/summary lines.

    I created a Parameter Field in the CR IDE. In the "Section Expert", I selected "Detail" and clicked on the "Suppress (No drill-down) checkbox. I then clicked on the Formula Editor icon next to it to enter the Formula Editor. I double-clicked on the Parameter Field and entered the following line for the formula:

    {?ReportType} = "D"

    My VB Code looks like this:
    Code:
        Public Function CreateSettlementReport() As Exception
            Try
                Dim crSettlement As New rptSettlement
                Dim diskOpts As DiskFileDestinationOptions = ExportOptions.CreateDiskFileDestinationOptions()
                Dim exportOpts As ExportOptions = New ExportOptions()
    
                crSettlement.SetParameterValue("@ReportType", strReportType)
                crSettlement.SetDataSource(DS)
    
                exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
                exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
    
                strSettlementReport = "\Settlement" & strReportDate & ".PDF"
    
                diskOpts.DiskFileName = strPath & strSettlementReport
                exportOpts.ExportDestinationOptions = diskOpts
    
                crSettlement.Export(exportOpts) 'Exporting report to a PDF format
    
                crSettlement = Nothing
                Return Nothing
    
            Catch ex As Exception
                strProcedure = "CreateSettlementReport()"
                strMsg = ex.Message
                WriteToErrorFile(strMsg, strProcedure, strProgram, intRecordCount)
                Return ex
            End Try
    My app is bombing on the highlighted line of code giving me the following error:

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

    I have know idea what this means. The variable strReportType is the Command Line parameter.

    Please help!

    Thanks,
    Blake

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Using Parameters w/Crystal Reports XI?

    The parameter name "@ReportType" does not exist. Try "?ReportType".

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    still the same error Bruce...
    Blake

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Using Parameters w/Crystal Reports XI?

    I would just do ReportType as that is the parameter name right?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    Lively Member
    Join Date
    Sep 2007
    Location
    Texas
    Posts
    98

    Re: Using Parameters w/Crystal Reports XI?

    I know its more work but the following always seems to work for me. Maybe you could try this:

    Code:
     Dim pfs As ParameterFields
     Dim pf As ParameterField
    Dim pdv As ParameterDiscreteValue
    
     pf.Name = "@ReportType"
     pdv.Value = strReportType
     pf.CurrentValues.Add(pdv)
    pfs.Add(pf)
    and then call your report using "pfs"

    Its a bit more code but, to be honest with you, since i started setting the parameters up this way i have never had any problems.

    Hope this helps

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Gary,

    Not to sound to stupid but how do I call my CR using the "pfs" Object?

    Thanks,
    Blake

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Using Parameters w/Crystal Reports XI?

    Blake

    Here is what I do to call (displaying on a View)

    vb.net Code:
    1. Public Sub DisplayReport(ByVal mRepName As String, ByVal mstrW As String, Optional ByVal strPars As String = "")
    2.         Dim mRep As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    3.         Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
    4.         Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
    5.         Dim currValue As New CrystalDecisions.Shared.ParameterValues
    6.         Dim mySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
    7.         Dim mySubRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    8.         Dim i, x, z As Integer 'Some Counters
    9.  
    10.         Try
    11.             ' Load the report
    12.             'Need to add the report dll yet Nothing Works
    13.             Dim rep As New RSMReports.clsReports(mRepName)
    14.             mRep = rep.returnRep()
    15.             mRep.RecordSelectionFormula = mstrW
    16.  
    17.  
    18.             'SQL Server
    19.             ConInfo.ConnectionInfo.UserID = MyUserID Here
    20.             ConInfo.ConnectionInfo.Password = My Password Here
    21.             ConInfo.ConnectionInfo.ServerName = mdlGeneral.serverName 'Stored Server name taht the database is on (using SQL Server)
    22.             ConInfo.ConnectionInfo.DatabaseName = mdlGeneral.dbName  '(Database to use on the Server)
    23.  
    24.             For i = 0 To mRep.Database.Tables.Count() - 1
    25.                 mRep.Database.Tables(i).ApplyLogOnInfo(ConInfo)
    26.             Next i
    27.  
    28.             'Check for Subreprots
    29.             For i = 0 To mRep.ReportDefinition.Sections.Count - 1
    30.                 For x = 0 To mRep.ReportDefinition.Sections(i).ReportObjects.Count - 1
    31.                     With mRep.ReportDefinition.Sections(i)
    32.                         If .ReportObjects(x).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
    33.                             mySubReportObject = CType(.ReportObjects(x), CrystalDecisions.CrystalReports.Engine.SubreportObject)
    34.                             mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
    35.                             For z = 0 To mySubRepDoc.Database.Tables.Count - 1
    36.                                 mySubRepDoc.Database.Tables(z).ApplyLogOnInfo(ConInfo)
    37.                             Next z
    38.                         End If
    39.                     End With
    40.                 Next x
    41.             Next i
    42.  
    43.             ' Set the report source for the crystal reports viewer to the report instance.
    44.             Me.crViewer.ReportSource = mRep
    45.             If strPars.Trim() <> String.Empty Then
    46.                 Dim arPars As String() = strPars.Split("~")
    47.                 Dim strVal As String()
    48.                 For xx As Integer = 0 To arPars.GetUpperBound(0)
    49.                     strVal = arPars(xx).Split("=")
    50.                     paraValue.Value = strVal(1)
    51.                     currValue = mRep.DataDefinition.ParameterFields(strVal(0)).CurrentValues
    52.                     currValue.Add(paraValue)
    53.                     mRep.DataDefinition.ParameterFields(xx).ApplyCurrentValues(currValue)
    54.                 Next
    55.             End If
    56.             ' Zoom viewer to fit to the whole page so the user can see the report
    57.             Me.crViewer.Zoom(100)
    58.             'Me.crViewer.ShowRefreshButton = False
    59.             Me.crViewer.DisplayGroupTree = False
    60.         Catch crExp As CrystalDecisions.CrystalReports.Engine.LoadSaveReportException
    61.             MessageBox.Show("Error printing Report " & mRepName & System.Environment.NewLine & _
    62.                 "The error number is: " & Err.Number.ToString() & System.Environment.NewLine & _
    63.                 "The error Message is: " & System.Environment.NewLine & crExp.Message.Trim(), "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    64.         Catch ex As Exception
    65.             MessageBox.Show("Error printing Report " & mRepName & System.Environment.NewLine & _
    66.                             "The error number is: " & Err.Number.ToString() & System.Environment.NewLine & _
    67.                             "The error Message is: " & System.Environment.NewLine & ex.Message.Trim(), "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    68.         End Try
    69.  
    70.     End Sub
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Gary,

    I played around with your previous code and understand it a little better now. I used in my procedure where I call my report. In my CR, I have defined a Parameter field called "ReportType". I know when a parameter field is reference within a CR it's format is ?ReportType. I'm not sure if that needs to be reflected in VB code when creating a parm field. See below
    Code:
        Public Function CreateSettlementReport() As Exception
            Try
                Dim crSettlement As New rptSettlement
                Dim diskOpts As DiskFileDestinationOptions = ExportOptions.CreateDiskFileDestinationOptions()
                Dim exportOpts As ExportOptions = New ExportOptions()
    
                Dim pf As New ParameterField
                Dim pdv As New ParameterDiscreteValue
    
                pf.Name = "ReportType"
                pdv.Value = strReportType     'The value is "D"
                pf.CurrentValues.Add(pdv)
    
                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)
                    .ParameterFields.Add(pf)
                    .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
    When the highlighted line of code is executed...it errors out with the message

    "Not Supported".

    I think I'm getting closer.
    Blake

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Using Parameters w/Crystal Reports XI?

    In the code I showed I build the paramert set in and other sub and they are passed into the procedure (strPars).

    The part that builds the parameters is based on the particular report. Say I have two parameters in the report MyTestRep with the name Text1 and Text2

    I build a string (lets call it strTempPars) like this
    "Text1='" & someTextHere & "'~Text2='" Someother text here & "'"

    No I call my procedure I pass the report name any selection statement (strW) and the parameter string (strPars)
    this section of code process the strPars and breaks them down to a pair of text string. (the parameter name and the parameter value)
    vb.net Code:
    1. If strPars.Trim() <> String.Empty Then
    2.                 Dim arPars As String() = strPars.Split("~")
    3.                 Dim strVal As String()
    4.                 For xx As Integer = 0 To arPars.GetUpperBound(0)
    5.                     strVal = arPars(xx).Split("=")
    6.                     paraValue.Value = strVal(1)
    7.                     currValue = mRep.DataDefinition.ParameterFields(strVal(0)).CurrentValues
    8.                     currValue.Add(paraValue)
    9.                     mRep.DataDefinition.ParameterFields(xx).ApplyCurrentValues(currValue)  'This actuall sets the parameter field in CR
    10.                 Next
    11.             End If
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Gary,

    I'm not the best programmer around and unfortunately there just isn't enough code there to help me understand...sorry!
    Blake

  11. #11
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Using Parameters w/Crystal Reports XI?

    OK

    So strPars = "Text1=I want this is in my First Parameter Place holder on the report~Text2=I Want this in the second parameter"

    In CR on the report I add two Parameters Named Text1 as Text and Text2 as Text.

    Now the code piece where you apply parameters.

    Look at strPars if it is not empty then we start
    Dim arPars as String() This will hold the Parameter Name and Value Pairs
    Dim strVal As String() This will hold each Pair as we split then and apply then to the report.
    arPars = strPars.Split("~") Ok this just split the parameters apart into Pairs of Name and value
    strVal = arPars(postion in the array we are wrking with).Split("=") OK we now have splut apart the Name/Value Pair with the parameter Name in strVal(0) and the value to apply in strVal(1)


    paraValue and currVal were defined at the beining of the sub
    (Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue Dim currValue As New CrystalDecisions.Shared.ParameterValues)

    We set the parValue.Value (the stuff we want sent into the report to be displayed) to strVal(1)

    paraValue.Value = strVal(1)

    We define the parameter field we want to work with and set it to strVal(0) (the parameter name)

    currValue = mRep.DataDefinition.ParameterFields(strVal(0)).CurrentValues

    Now we add the text to that parameter we just defined

    currValue.Add(paraValue)

    Now we apply the whole thing to the report

    mRep.DataDefinition.ParameterFields(xx).ApplyCurrentValues(currValue)


    We enter a loop for 0 to the upperbound of arPars
    We now split the Pair apart geting the Parameter Name and the Parameter Value
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Ok,

    Now it's making a little more sense. But I want to use my code, applying your example. So with the following code the only problem I can see is the highlighted line of code. When I tried entering that line...there doesn't appear to be an "ApplyCurrentValues" Method. I even tried setting a variable as in your example and it still didn't have that Method.
    Code:
                pf.Name = "ReportType"
                pdv.Value = strReportType
                pf.CurrentValues.Add(pdv)
    
                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)
                    .DataDefinition.ParameterFields(pf, strSettlementReport)
                    .ParameterFields.Add(pf)
                    .Export(exportOpts)
                End With
    Blake

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Forget the last post Gary. I got it working syntactically speaking anyway.
    Blake

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Gary,

    I got the VB portion working (I think...I'm not getting any runtime errors). Now, in my CR, I've defined a Parameter Field named "ReportType". Now, take a look at the 2 screenshots and see if they look right.

    Thanks,
    Blake

  15. #15
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Using Parameters w/Crystal Reports XI?

    I think so.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    I would like to create a Formula field like this:
    Code:
    WhilePrintingRecords;
    
    If {?RecordType} = "S"
        HIDE DETAIL SECTION
    End If
    But I'm not sure how to write code where I can control the sections.
    Blake

  17. #17
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Using Parameters w/Crystal Reports XI?

    If {?RecordType} = "S" Then True Else False

    Use the Section Expert Click the Formula button next to Suppress (No Drill-Down). Enter that formula. IF S is passed in the Parameter then The secion is Suppresed (Hidden) other wise the section will show.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Using Parameters w/Crystal Reports XI?

    Time to party!!!

    That worked Gary...thanks for all your help!
    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