Parameters to a Crystal Report

I have an app written in VB6, and a Crystal Report in version 8.5 .  The report has some parameter fields, which when set up had some data attached.  This is so the report can pass those parameters to a SQL Server 2000 Stored Procedure in order to get the data back.  How do I pass new parameter values for those fields from my VB app?

I include References in my VB project to the Crystal Reports 8.5 Libary, and ActiveX Library. but unsure if I need any others, or if there is a better method.  If this is a better way of doing it, please let me know.  Also, please supply VB code.

I apologise if I've not explained this properly, so feel free to ask questions.  I may also up the points, depending on the complexity of the answer etc.

Thanks alot, Adi
Who is Participating?
BrightonConnect With a Mentor Commented:
Here is a little VB program that may answer your question

Dim crxApplication As New CRAXDRT.Application

Private Report As CRAXDRT.Report

Private Sub Form_Load()

    ' This program expects at lest 3 parameters to be passed to it
    ' and the program first parses the command line into a ParameterArray
    ' ParameterArray(1)   = Report to run
    ' ParameterArray(2)   = Output destination
    ' ParameterArray(3)   = What format is the output saved as
    ' ParameterArray(4+N) = Parameters that are passed to the crystal
    '                       report in the same order they are listed
    '                       in the report.
    ' The program them sets any parameters the are passed in to it and places
    ' these values in the parameters that are in the report.

    On Error GoTo ErrorHandle
    Dim linenbr As Integer
    Dim ParameterArray() As String
    Dim OutFile          As String      'ParameterArray(2)
    Dim OutputFormat     As Integer     '0 = No Format
                                        '1 = Crystal Report Format
                                        '8 = Text
                                        '31 = PDF
    Dim RC As Integer
    Dim Pcnt As Integer
    ' Fill the array with the command line passed in
    ' using the comma as a deliminator between fields.
    RC = FillArray(Command, ",", ParameterArray())
    Set crxApplication = New CRAXDRT.Application
    Dim crxParameterField As CRAXDRT.ParameterFieldDefinition
    Dim crxparameterFields As CRAXDRT.ParameterFieldDefinitions
    ' The first parameter passed in is the name of the
    ' report to run
    Set Report = crxApplication.OpenReport(Trim(ParameterArray(1)))
    ' The second parameter is the location where the
    ' the output file is to go
    OutFile = Trim(ParameterArray(2))
    Pcnt = Report.ParameterFields.Count
    If Pcnt > 0 Then
        Set crxParameterField = Report.ParameterFields.Item(1)
    End If
    ' Log on to the database
    For i = 1 To Report.Database.Tables.Count
        Report.Database.Tables(i).SetLogOnInfo "", "", "<<Use Integrated Security>>", ""
    Next i
    If Pcnt > 0 Then
        crxParameterField.EnableMultipleValues = True
        Call crxParameterField.ClearCurrentValueAndRange
    End If

    ' Read each parameter from the report and populate
    ' them in the same order they are read in from
    ' the command line.
    'crxParameterField.EnableNullValue = True
    Dim crnull As Variant
    For Each crxParameterField In Report.ParameterFields
        crxParameterField.AddCurrentValue crnull
    Pcnt = Report.ParameterFields.Count
    If Pcnt > 0 Then
        For x = 1 To Pcnt
          If ParameterArray(x + 3) = "" Then
             Report.ParameterFields(x).AddCurrentValue crnull
          ElseIf Report.ParameterFields(x).ValueType = crNumberField Then
             Report.ParameterFields(x).AddCurrentValue CDbl(Trim(ParameterArray(x + 3)))
             Report.ParameterFields(x).AddCurrentValue CStr(Trim(ParameterArray(x + 3)))
          End If
        Next x
    End If
    Select Case Trim(CInt(ParameterArray(3)))
     Case Is = 0
      OutputFormat = 0  ' No Format
     Case Is = 1
      OutputFormat = 1  ' Crystal Report Format
     Case Is = 8
      OutputFormat = 8  ' Text
     Case Is = 31
      OutputFormat = 31 ' crEFTPortableDocFormat (PDF)
    End Select
    If OutputFormat = 0 Then
        RptName.Caption = Trim(ParameterArray(1))
        Report.DisplayProgressDialog = False
        Report.ExportOptions.DestinationType = crEDTDiskFile
        Report.ExportOptions.FormatType = OutputFormat
        Report.ExportOptions.DiskFileName = OutFile
        Report.Export False
    End If

    Err.Number = 0
    Unload ExecCR
    Exit Sub
    Label1.Caption = linenbr
    Label2.Caption = Err.Number
    Label3.Caption = Err.Description
    Debug.Print Err.Number
    Err.Number = 1
    Unload ExecCR
End Sub

Function FillArray(CommandLineIn As String, Delimiter As String, aValues() As String) As Integer

     Dim ArrayCount As Integer
     Dim CurrentPosInArray As Integer
     Dim LenghtOfVariable As Integer
     Dim StringLength As Integer

     ArrayCount = 1
     CurrentPosInArray = 1
     LenghtOfVariable = 1

     StringLength = Len(CommandLineIn)
           ReDim Preserve aValues(1 To ArrayCount) As String

           StringLength = (InStr(CurrentPosInArray, CommandLineIn, Delimiter) - CurrentPosInArray)

           If StringLength < 0 Then

                 aValues(ArrayCount) = Right$(CommandLineIn, (Len(CommandLineIn) - (LenghtOfVariable - 1)))

                 Exit Do


                  aValues(ArrayCount) = Mid$(CommandLineIn, CurrentPosInArray, StringLength)

           End If

           LenghtOfVariable = LenghtOfVariable + (Len(aValues(ArrayCount)) + Len(Delimiter))
           CurrentPosInArray = LenghtOfVariable
           ArrayCount = ArrayCount + 1

     If ArrayCount = 2 Then
        ArrayCount = 3
        ReDim Preserve aValues(1 To ArrayCount) As String
        aValues(3) = 8
        'aValues(4) = 1
     End If

     FillArray = UBound(aValues)

End Function

I only pass selection queries and formulas to my crystal reports, but I assume it is very similar to passing formulas.

I'd try this:
IPRPT.ParameterFields(0) = "Parm1=2"

Where IPRPT is the name of the CR activeX control on the form and Parm1 is the name of the CR parmater.  If you need to set another parmater it would be the same excpt use ParameterFields(1) and keep incrementing the value of ParameterFields for each parameter.
Good guess but the format is a little different

IPRPT.ParameterFields(0) = "Parm1=2"

IPRPT.ParameterFields(0) = "ParmName;Value;True"

IPRPT.ParameterFields(0) = "Parm1;2;TRUE"

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

A little clarification

The generic version
IPRPT.ParameterFields(0) = "ParmName;Value;True"

Example based on the supplied code.
IPRPT.ParameterFields(0) = "Parm1;2;TRUE"

The easiest way to run a crystal report from VB, and to set parameters is to use a Crystal Report Control.  So, under the Project|Components, you should pick the Crystal Report Control (viewer, whatever).

Then, in your code you'd have something like:

Report1.ReportFile = "C:\Code\MyRpt.rpt"
Report1.StoredProcParams(0) = 123
Report1.StoredProcParams(1) = chr$(34) & "Jones" & chr$(34)
Report1.Action = 1

The first question is about the nature of the parameters you mentioned.  In Crystal, there are two different kinds of parameters.  If you based your report on a stored proc, and the stored proc has parameters, then Crystal Creates StoredProcParams, which can be set using the code above.

If the developer went into Crystal and declared some Parameters there, then as mentioned above, I think this is referenced by Report1.ParametersFields(0)

My preference is not to work with Crystal's parameters, but only work with the ones generated for the Stored Procs Parameters.  When filling these parameters, make sure that they are formatted appropriately for the type of database you're accessing.  I wrapped the text parameter in double-quotes above, and the easiest way to do this is by appending the chr$(34).
adrianjohnsonAuthor Commented:
Thanks for the code.  Got the report to work now, thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.