cancel
Showing results for 
Search instead for 
Did you mean: 

Code in vb.net to pass multiple values to parameter fields

Former Member
0 Kudos

I have designed a report in which it has a month parameter which accepts multiple values. Can anyone tell me how to pass multiples values selected by user in the interface to crystal reports?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello,

One way is to use the code found in KB [1273690 - Pass multiple parameter values to one parameter in a Crystal Report|https://bosap-support.wdf.sap.corp/sap/support/notes/1273690].

If you can't visit that link here's the code from the article:

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Public Class Form1
Inherits System.Windows.Forms.Form

''CR Variables   
Dim crReportDocument As ReportDocument
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As ParameterValues
Dim crParameterDiscreteValue As ParameterDiscreteValue 
Dim reportPath As String = Application.StartupPath & "\" & "CustomersByCountry.rpt"

Friend WithEvents CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer

#Region " Windows Form Designer generated code "
       Public Sub New()
       MyBase.New()
           'This call is required by the Windows Form Designer.
       InitializeComponent()

'Add any initialization after the InitializeComponent() call
'Create an instance of the report object
crReportDocument = New ReportDocument()
crReportDocument.Load(reportPath)

'Get the collection of parameters from the report
crParameterFieldDefinitions = crReportDocument.DataDefinition.ParameterFields

'Access the specified parameter from the collection
crParameterFieldDefinition = crParameterFieldDefinitions("Country")

'Get the current values from the parameter field.  At this point
'there are zero values set.
crParameterValues = crParameterFieldDefinition.CurrentValues

'Set the current values for the parameter field
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = "Canada" '1st current value

'Add the first current value for the parameter field
crParameterValues.Add(crParameterDiscreteValue)

'Since this parameter allows multiple values, the discrete value
'object needs to be reset.  Destroy the previous instance and create
'a new instance.
crParameterDiscreteValue = Nothing
crParameterDiscreteValue = New ParameterDiscreteValue()
crParameterDiscreteValue.Value = "USA" '2nd current value

'Add the second current value for the parameter field
crParameterValues.Add(crParameterDiscreteValue)

'All current parameter values must be applied for the parameter field.
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

'Set the viewer to the report object to be previewed.
CrystalReportViewer1.ReportSource = crReportDocument

End Sub

Sincerely,

Dan Kelleher

Former Member
0 Kudos

My reprot has week parameter where user can select more than one week, i wrote the below code,

Dim crParameterDiscreteValue As ParameterDiscreteValue

Dim crParameterFieldDefinitions As ParameterFieldDefinitions

Dim crParameterFieldLocation As ParameterFieldDefinition

Dim crParameterValues As ParameterValues

Dim crParameterFieldDefinition As ParameterFieldDefinition

crParameterFieldDefinitions = cryrpt.DataDefinition.ParameterFields

crParameterFieldDefinition = crParameterFieldDefinitions("Week")

crParameterValues = crParameterFieldDefinition.CurrentValues

crParameterDiscreteValue = New ParameterDiscreteValue()

crParameterDiscreteValue.Value = "18" '1st current value

crParameterValues.Add(crParameterDiscreteValue)

'crParameterDiscreteValue = Nothing

crParameterDiscreteValue = New ParameterDiscreteValue()

crParameterDiscreteValue.Value = "17" '2nd current value

crParameterValues.Add(crParameterDiscreteValue)

crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

'crParameterFieldLocation.ApplyCurrentValues(crParameterValues)

But the rpeort is showing only records in week 17 only. whatever value i am assigning in the last it is only getting passed. Can anyone please help me?

Former Member
0 Kudos

Hello,

In your code you have a line of code that shouldn't be commented out:

'crParameterDiscreteValue = Nothing

Uncomment that line of code and test again. You should also be able to move the code I posted earlier into a simple sample application (outside of your solution) and test that code to see if it works.

If you're still having trouble after that you'll need to post details about your environment.

Which version of Visual Studios?

Which version of Crystal Reports?

Operating system of your development machine?

Which Crystal patches have you applied (if any)?

Is it a Windows or Web application?

Sincerely,

Dan Kelleher

Former Member
0 Kudos

I have created a sample application and tried the code, then also it is showing only the last parameter values.

Below are the version details,

Visaul studio 2008

Crystal reprots 2008 SP3

Windows XP

Windows application

Former Member
0 Kudos

Hello,

The code in the KB works fine - I just retested it with a sample report in a new sample application. Perhaps there's an issue with the parameter in your report. Is the parameter really set up to allow multiple values, etc?

If you open the report in Crystal Reports 2008 directly and refresh the report you should see a prompting panel that allows you to select more than one entry. Is this what you see? Can you select "week 17' and "week 18" and view the correct results from the designer?

Sincerely,

Dan Kelleher

Former Member
0 Kudos

I am extremely sorry, i gave wrong reprot path in database. Now it is working fine. Thanks a lot.

Former Member
0 Kudos

Hi Daniel Kelleher

I have some piece of code and even I want to know that how to pass two parameters with different fields with different values,

can you please visit this link so you can come to know that what exactly I mean

Link : http://scn.sap.com/thread/3583400

Answers (0)