Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crystal Report Viewer - Set Report Title

Status
Not open for further replies.

1SorryDog

Technical User
Jul 15, 2002
25
0
0
US
I'm using vb.net 2005 with crystal XI which I'm new at.

I have a report viewer form called CrystalReportViewer1

My report runs fine with:

frmReportViewer.CrystalReportViewer1.ReportSource = strReport '(Path to saved report file)
frmReportViewer.CrystalReportViewer1.DisplayToolbar = True
frmReportViewer.CrystalReportViewer1.SelectionFormula = MySelectionFormula

frmReportViewer.Show()

I want to pass a date and title to the report at runtime.

Can someone point me in the right direction please?

Thanks
 
Thanks tperri

I'm still missing something.
I already have a report source as from above.

So I'm setting a new report source?

I have this code from one of the links you provided above.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
'
' Load the selected report file.
'
Dim CR As New ReportDocument
CR.Load(strReportPath)
'
' Declare the parameter related objects.
'
Dim crParameterDiscreteValue As ParameterDiscreteValue
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldLocation As ParameterFieldDefinition
Dim crParameterValues As ParameterValues
'
' Get the report's parameters collection.
'
crParameterFieldDefinitions = CR.DataDefinition.ParameterFields
'
' Set the first parameter
' - Get the parameter, tell it to use the current values vs default value.
' - Tell it the parameter contains 1 discrete value vs multiple values.
' - Set the parameter's value.
' - Add it and apply it.
' - Repeat these statements for each parameter.
'
crParameterFieldLocation = crParameterFieldDefinitions.Item("StartDate")
crParameterValues = crParameterFieldLocation.CurrentValues
crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
crParameterDiscreteValue.Value = strStartDate
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldLocation.ApplyCurrentValues(crParameterValues)
'
' Set the Crytal Report Viewer control's source to the report document.
'
CrystalReportViewer.ReportSource = CR



How should I code this? Sorry if this is elementary.

 
So are you trying to change the text that's in the Tab that shows the report title? I had the same issue where the tab was saying "Main Page" or something like that, I found this code that would allow me to change it:

CType(rptViewer.Controls(0).Controls(0), System.Windows.Forms.TabControl).TabPages.Item(0).Text = "Enter Title Here
 
BBousman,

No. I can change the text of the tabs.

At the top of my report I have a field called Report Title which I want to set the title at runtime of my app. The title would read something like: "Main Plant - 02/21/2008"

I just don't know how to send the title text to the report when I already have a report source.

It's a big jump when going from vb6 and crystal 6 to vb.net 2005 and crystal 11.

Thanks for the tip though.
 
Have you seen what is available to be set for the ReportDucoment CR?

Use intellisense to see if there is a title property that can be set?

Sorry to ask this, if you have gone over this, forgive me for asking.

 
tperri,

No problem.
The report is a an existing cr 6 report that wasn't created by me, however I have made changes to it in the past. It does have a field at the top of the report which is named ReportTitle1. Basically I need to send the title along with a date range (see below) to the report.

So I'm confused with the example above and others on the net show setting a report source which I already have one. My current report source is a date range for the report which is selected from my app by the user.

Thanks again for your help.



 

Try this. It can go anywhere before the line that sets the ReportSource.

Code:
CR.SummaryInfo.ReportTitle = "Main Plant - 02/21/2008"

Pat
 
pmegan,

Thanks for the tip but .SummaryInfo is not avaliable.

References:
Imports VB = Microsoft.VisualBasic
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms


My Code:
Find = "{PROD.DATE} >= Date (" & StartDate & ") and " & "{PROD.DATE} <= Date (" & EndDate & ")"

'Clear previous report source
frmReportViewer.CrystalReportViewer1.ReportSource = Nothing

'Enable Toolbar
frmReportViewer.CrystalReportViewer1.DisplayToolbar = True

'Create the parameter value objects
Dim paramStartDate As New ParameterDiscreteValue()
paramStartDate.Value = "02/25/2008" 'Static test string

'Create the parameters collection
Dim paramList As New ParameterFields()

'Create the parameter objects and add them to the collection
Dim paramTemp As ParameterField

paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "ReportTitle1" 'Object on report is named ReportTitle1
paramTemp.CurrentValues.Add(paramStartDate)
paramList.Add(paramTemp)

'Assign parameters collection to the report viewer
frmReportViewer.CrystalReportViewer1.ParameterFieldInfo = paramList

I get an error on the line above which read:
"InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index"

'Load the report
frmReportViewer.CrystalReportViewer1.ReportSource = "c:\prod\Prod.rpt"
frmReportViewer.CrystalReportViewer1.SelectionFormula = Find
frmReportViewer.Show()



Thanks for any help with this report.
 
You may be wiping out the parameters you set up by loading the report source that late in the procedure. Assigning data spec to the viewer never worked well for me. Generally loading the report into a ReportDocument instance and manipulating it there does better. Then you just assign the ReportDocument as the viewer's source.
I stopped using parameters in my reports ages ago so I'm a little rusty, but something like this should work:


Code:
[green]' create the ReportDocument and Parameter instances[/green] 
Dim CR As New ReportDocument
Dim pStart As New ParameterField()
Dim dEnd as new ParameterField()
[green]' set the selection formula[/green]
Dim Find as String = "{PROD.DATE}  >=  Date (" & StartDate & ") and   " & "{PROD.DATE} <= Date (" & EndDate & ")"

Dim dStart as Date = CDate("02/25/2008")[green]' hardcoded for testing[/green]

[green]' add the StartDate parameter[/green]
With pStart
	.Name = "StartDate"
	.ParameterValueType = ParameterValueKind.DateParameter
End With

[green]' add the EndDate parameter[/green]
With pStart
	.Name = "EndDate"
	.ParameterValueType = ParameterValueKind.DateParameter
End With

With CR
	[green]' load the report definition[/green]
	.Load("c:\prod\Prod.rpt")

	[green]' add the date parameters[/green]
	.ParameterFields.Add(pStart)
	.ParameterFields.Add(pEnd)

	[green]' set the report title[/green]
	.SummaryInfo.ReportTitle = "New Report Title Goes Here"

	[green]' set the record selection & parameter values[/green]
	.DataDefinition.RecordSelectionFormula = Find
	.SetParameterValue("StartDate", dStart)
	.SetParameterValue("EndDate", dStart)
End With

[green]' assign the ReportDocument as the viewer source and show it[/green]
frmReportViewer.CrystalReportViewer1.ReportSource = CR
frmReportViewer.Show()

It would probably be a whole lot easier if you can add the parameters to the report template instead of adding them in on the fly too.

If you have privileges to write stored procedures on teh database you may want to look into going the "data push" route instead. It's more work to set up, but the database server does most of the processing instead of the report template and the amount of data dragged across the network will be reduced quite a bit, so your app should run a little quicker.

Pat


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top