I want to insert a string into a crystal report field. It is the user's actual name. What type of field do I need on the Crystal Report, and how do I tell VB to send this string to that field?
Printable View
I want to insert a string into a crystal report field. It is the user's actual name. What type of field do I need on the Crystal Report, and how do I tell VB to send this string to that field?
Insert a parameter in your report
then when you call the report in VB
go through the parameters collection for the report and set the new parameter to the required value.
Regards
Chris
Could you show me an example of how to do that?Quote:
Originally posted by chrisa_uk
go through the parameters collection for the report and set the new parameter to the required value.
There are a few ways to do this, but I use the following as I am not sure how to use the others.
Firstly your project needs to reference craxdrt.dll and and have component crviewer.dll. Now add a crystal report viewer to your form from the tool box.
you need the following declarations
Public reportToView As CRAXDRT.Report
Public subReportToView As CRAXDRT.Report
Public reportApp As New CRAXDRT.Application
Public params As CRAXDRT.ParameterFieldDefinitions
Public param As CRAXDRT.ParameterFieldDefinition
and then when you load the form:-
'insert your report name and path in the brackets
Set reportToView = reportApp.OpenReport(App.Path + "\summary.rpt", 1)
'if you have a sub report with parameters then you also need to open a sub report
Set subReportToView = reportToView.OpenSubreport("summary")
reportToView.EnableParameterPrompting = False
reportToView.ReadRecords
Set params = reportToView.ParameterFields
For Each param In params
With param
.ClearCurrentValueAndRange
'my parameter in CR is called startDate
If .Name = "{?startDate}" Then
'send your value here mine is a date
.AddCurrentValue CDate(Format(startDate, "dd-mmm-yyyy"))
Else
'I have another parameter in my report (endate) the parameter must be this
.AddCurrentValue CDate(Format(endDate, "dd-mmm-yyyy"))
End If
End With
Next
'open the sub report
reportToView.OpenSubreport ("summary")
'same agaim for parameters
Set params = subReportToView.ParameterFields
For Each param In params
With param
.ClearCurrentValueAndRange
If .Name = "{?startDate}" Then
.AddCurrentValue CDate(Format(startDate, "dd-mmm-yyyy"))
Else
.AddCurrentValue CDate(Format(endDate, "dd-mmm-yyyy"))
End If
End With
Next
'display the report
With rptExpenses
.ReportSource = reportToView
.ViewReport
.Zoom 1
End With
Hope this helps
Regards
Chris
THANK YOU!!! I have a good start, but I need to use 2 parameters, and I'm not sure how to do this.
I have to have the following:
?OrderDate = sOrderDate (which is working fine)
AND
?ATMID = sATMID (how do I add this in?)
Thank you again! You are a mindsaver!
Here is the code that I have for the parameters section of the code to explain that a little better:
For Each param In params
With param
.ClearCurrentValueAndRange
If .Name = "{?OrderDate}" Then
.AddCurrentValue CDate(Format(sOrderDate, "dd-mmm-yyyy"))
End If
If .Name = "{?ATMID}" Then
.AddCurrentValue sOrderATMID
End If
If .Name = "{?UserName}" Then
.AddCurrentValue strFullName
End If
End With
Next
The report is pulling the correct orderdate from the database, and the username is correct, but it is pulling all the ATM's for the orderdate, not just the one stored in the sOrderATM variable.
It is giving the correct information, but it is giving 2 pages of the same information. Do you have any idea why?
Does it give 2 pages of the same info if you open it in Crystal Reports or only in VB?
Regards
Chris