jasonboetcher
asked on
Printing a Crystal Report
I want to print a report from VB 6 code. How do I do it? Here is what I have tried:
rptCrystal.ReportFileName = sReportName
CRViewer1.ReportSource = rptCrystal
CRViewer1.PrintReport
rptCrystal is the Crystal Reports control
sReportName is the fully qualified name of the actual Crystal Report I want to print
CRViewer1 is the Crystal Reports Viewer control
Any help would be greatly appreciated.
rptCrystal.ReportFileName = sReportName
CRViewer1.ReportSource = rptCrystal
CRViewer1.PrintReport
rptCrystal is the Crystal Reports control
sReportName is the fully qualified name of the actual Crystal Report I want to print
CRViewer1 is the Crystal Reports Viewer control
Any help would be greatly appreciated.
All I use is:
CRViewer1.ReportSource = Report2Preview
Report2Preview.DiscardSave dData
CRViewer1.ViewReport
Then let the user decide to print or not to print :)
A clarification... When you say that
"sReportName is the fully qualified name of the actual Crystal Report I want to print"
do you mean you are sending the crviewer a string like "c:\apps\report.rpt"?
If so this your problem. You need to get your report into your VB project to be able to use in in the CRViewer. To do this you'll need to right click in the project window and select Insert/crystal report. You'll next be prompted with a report generation wizard. Select Import an Existing Report. Select your report. It will now be in inside VB as a "Designer". Here, check the reports properties window (F4) and fill in the Name property. Now you are ready to use the viewer component by using this new name in the "crviewer1.reportsource = " line.
CRViewer1.ReportSource = Report2Preview
Report2Preview.DiscardSave
CRViewer1.ViewReport
Then let the user decide to print or not to print :)
A clarification... When you say that
"sReportName is the fully qualified name of the actual Crystal Report I want to print"
do you mean you are sending the crviewer a string like "c:\apps\report.rpt"?
If so this your problem. You need to get your report into your VB project to be able to use in in the CRViewer. To do this you'll need to right click in the project window and select Insert/crystal report. You'll next be prompted with a report generation wizard. Select Import an Existing Report. Select your report. It will now be in inside VB as a "Designer". Here, check the reports properties window (F4) and fill in the Name property. Now you are ready to use the viewer component by using this new name in the "crviewer1.reportsource = " line.
I also have a sample app at www.angelfire.com/nt/gordonjones/vb.html
ASKER
The way I want to do this does not require that the report be previewed by a user. There really is no user as the system is a task running on an NT service. After data has been imported into the database I need the crystal reports to generate and print. I apologize for not being clear about this.
I have tried the following code:
rptCrystal.ReportFileName = sReportName
With CRViewer1
.ReportSource = rptCrystal
.ViewReport
Screen.MousePointer = vbDefault
DoEvents
.Zoom (85)
.PrintReport
End With
I get the following error on the ".ReportSource = rptCrystal" line:
"Run Time Error '430': Class does not support automation or does not support expected interface."
How can I fix this?
I have tried the following code:
rptCrystal.ReportFileName = sReportName
With CRViewer1
.ReportSource = rptCrystal
.ViewReport
Screen.MousePointer = vbDefault
DoEvents
.Zoom (85)
.PrintReport
End With
I get the following error on the ".ReportSource = rptCrystal" line:
"Run Time Error '430': Class does not support automation or does not support expected interface."
How can I fix this?
I repeat :
A clarification please... When you say that
"sReportName is the fully qualified name of the actual Crystal Report I want to print"
do you mean you are sending the crviewer a string like "c:\apps\report.rpt"?
If so this your problem. You need to get your report into your VB project to be able to use in in the
CRViewer. To do this you'll need to right click in the project window and select Insert/crystal report.
You'll next be prompted with a report generation wizard. Select Import an Existing Report. Select your
report. It will now be in inside VB as a "Designer". Here, check the reports properties window (F4)
and fill in the Name property. Now you are ready to use the viewer component by using this new name
in the "crviewer1.reportsource = " line.
A clarification please... When you say that
"sReportName is the fully qualified name of the actual Crystal Report I want to print"
do you mean you are sending the crviewer a string like "c:\apps\report.rpt"?
If so this your problem. You need to get your report into your VB project to be able to use in in the
CRViewer. To do this you'll need to right click in the project window and select Insert/crystal report.
You'll next be prompted with a report generation wizard. Select Import an Existing Report. Select your
report. It will now be in inside VB as a "Designer". Here, check the reports properties window (F4)
and fill in the Name property. Now you are ready to use the viewer component by using this new name
in the "crviewer1.reportsource = " line.
ASKER
Yes, I am passing a string such as "c:\etc\etc.rpt". This is how I have done this in the past. If I import a report into the project does this make it part of the executable? I wouldn't want to do this.
Here are the declarations I put in a standard module.
I put them here because my app uses the one report form containing the viewer for all of its printed output.
Public ReportActive As Boolean 'Is a crystal report open
Public appCrystal As New CRAXDRT.Application 'Crystal application object
Public rptCrystal As New CRAXDRT.Report 'Crystal report object
Public pstReportName As String 'Crystal report name string
Public pstSelection As String 'Selection criteria for crystal report
Public pstTitle As String 'Title name for crystal
At the time of this post I do not use the ReportActive variable.
Typical code behind a button on another form that opens a certain report
pstReportName = "\\GREENHOUSE\shrdata\Shr_ Data\Datab ase\Servic e\NonConfo rm.rpt"
pstSelection = "{dbo_WorkOrder.Shipdate} in Date (" & Year(mskStartDt.Text) & ", " & Month(mskStartDt.Text) & ", " & Day(mskStartDt.Text) & ") to Date (" & Year(mskEndDt.Text) & ", " & Month(mskEndDt.Text) & ", " & Day(mskEndDt.Text) & ")" & " and {dbo_WorkOrder.Warranty} = " & "'YES'"
pstTitle = ""
frmReport.Show vbModal
Form Load event of the form containing the crystal viewer control
Screen.MousePointer = vbHourglass
Set appCrystal = CreateObject("CrystalRunti me.applica tion")
Set rptCrystal = appCrystal.OpenReport(pstR eportName)
'Setup the report
With rptCrystal
.RecordSelectionFormula = pstSelection
.ReportTitle = pstTitle
End With
With CRViewer1
.ReportSource = rptCrystal
.ViewReport
Screen.MousePointer = vbDefault
DoEvents
.Zoom (85)
End With
Set rptCrystal = Nothing
Set appCrystal = Nothing
References made to
Crysal Report 7 ActiveX Designer Run Time Library
Crystal Report Smart Viewer
Instead of .viewreport (which lets the user decide what to do) you could use .printreport
That should give you what you need.
Andy
I put them here because my app uses the one report form containing the viewer for all of its printed output.
Public ReportActive As Boolean 'Is a crystal report open
Public appCrystal As New CRAXDRT.Application 'Crystal application object
Public rptCrystal As New CRAXDRT.Report 'Crystal report object
Public pstReportName As String 'Crystal report name string
Public pstSelection As String 'Selection criteria for crystal report
Public pstTitle As String 'Title name for crystal
At the time of this post I do not use the ReportActive variable.
Typical code behind a button on another form that opens a certain report
pstReportName = "\\GREENHOUSE\shrdata\Shr_
pstSelection = "{dbo_WorkOrder.Shipdate} in Date (" & Year(mskStartDt.Text) & ", " & Month(mskStartDt.Text) & ", " & Day(mskStartDt.Text) & ") to Date (" & Year(mskEndDt.Text) & ", " & Month(mskEndDt.Text) & ", " & Day(mskEndDt.Text) & ")" & " and {dbo_WorkOrder.Warranty} = " & "'YES'"
pstTitle = ""
frmReport.Show vbModal
Form Load event of the form containing the crystal viewer control
Screen.MousePointer = vbHourglass
Set appCrystal = CreateObject("CrystalRunti
Set rptCrystal = appCrystal.OpenReport(pstR
'Setup the report
With rptCrystal
.RecordSelectionFormula = pstSelection
.ReportTitle = pstTitle
End With
With CRViewer1
.ReportSource = rptCrystal
.ViewReport
Screen.MousePointer = vbDefault
DoEvents
.Zoom (85)
End With
Set rptCrystal = Nothing
Set appCrystal = Nothing
References made to
Crysal Report 7 ActiveX Designer Run Time Library
Crystal Report Smart Viewer
Instead of .viewreport (which lets the user decide what to do) you could use .printreport
That should give you what you need.
Andy
ASKER
abaldwin:
Your suggestion works with one minor exception. The .printreport method brings up a message box that must be clicked to actually print the report. How can I programatically tell the report to print without intervention?
Your suggestion works with one minor exception. The .printreport method brings up a message box that must be clicked to actually print the report. How can I programatically tell the report to print without intervention?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry left out a line
This
'Setup the report
With rptCrystal
.RecordSelectionFormula = pstSelection
.ReportTitle = pstTitle
.printout (False)
End with
Should be
'Setup the report
With rptCrystal
.RecordSelectionFormula = pstSelection
.ReportTitle = pstTitle
.ReadRecords
.printout (False)
End with
This
'Setup the report
With rptCrystal
.RecordSelectionFormula = pstSelection
.ReportTitle = pstTitle
.printout (False)
End with
Should be
'Setup the report
With rptCrystal
.RecordSelectionFormula = pstSelection
.ReportTitle = pstTitle
.ReadRecords
.printout (False)
End with
ASKER
Here is all I have and it works great:
Set appCrystal = CreateObject("CrystalRunti me.applica tion")
Set rptCrystal = appCrystal.OpenReport(sRep ortName)
rptCrystal.PrintOut (False)
What does the .ReadRecords do?
Set appCrystal = CreateObject("CrystalRunti
Set rptCrystal = appCrystal.OpenReport(sRep
rptCrystal.PrintOut (False)
What does the .ReadRecords do?
That is about the same thing.
The Recordselectionformula allows you to dynamically set the selection criteria at run time rather than making it part of the actual crysta report. I.E. Report should be between a certain set of dates.
The Report title does the same but changes the report title
In one of my apps I had to insert the line ReadRecords to insure that I was getting all the data not just the first page. Was kinda weird but that fixed it.
And of course you know what the printout does. :-)
If your app is going to print the same report and the criteria never changes then you will be fine.
andy
The Recordselectionformula allows you to dynamically set the selection criteria at run time rather than making it part of the actual crysta report. I.E. Report should be between a certain set of dates.
The Report title does the same but changes the report title
In one of my apps I had to insert the line ReadRecords to insure that I was getting all the data not just the first page. Was kinda weird but that fixed it.
And of course you know what the printout does. :-)
If your app is going to print the same report and the criteria never changes then you will be fine.
andy
ASKER
One more thing; if you don't know that's fine.....
How would I pass parameters into a report?
How would I pass parameters into a report?
rptCrystal.parameterfields (0) = 1 should set the first parameter field to 1
Indexed starting with 0
Andy
Indexed starting with 0
Andy
.ReportSource = rptCrystal
.ViewReport
Screen.MousePointer = vbDefault
DoEvents
.Zoom (85)
.printreport
End With
I think you have to let the report load first then print
I have a doevents and a zoom to 85% then the printreport.
Seems to work for me.