How to print Crystal Report directly?

IndraG
02-09-2007, 06:18 AM
I usually have a form in Visual Basic 6 with CR Viewer control, where I allow the user to preview the report and then print it through the CR Viewer. The user now want to print the report directly when he hits the OK button (which do other functions as well) without needing to view the report (which he trusts will be OK anyway). Unfortunately, none of my VB books as well as my Crystal Report books teach me how to do that. Any suggestion where to start first? I am using VB 6 and CR 8.5.

TavMaster-T
02-09-2007, 01:29 PM
First, add a reference to "Crystal Report Control" (Crystl32.ocx).

Declare a Crystal Report variable.
The rest should work like the control on the form.


Dim crReport As CrystalReport 'Crystal Report Control

crReport.ReportFileName = [Report File Name]
crReport.Destination = crptToPrinter
crReport..WindowTitle = "Report Name"
crReport.Action = 1


You can also use "crptToWindow" (instead of "crptToPrinter") to display the report in a window. If you do that then declare the variable as static or make it a global variable so that the report stays open when the code finishes. You may also need the set .connect property to the appropriate ODBC connect string.

Hope that helps.

IndraG
02-13-2007, 06:50 PM
Dear TavMaster-T,
I tried your suggestion, with this code:

Private Sub cmdPrint_Click()
Dim Report As CrystalReport
Dim strTest As String

Report.ReportFileName = "C:\RPT\Faktur PAS.rpt"

strTest = "{tblDetailOrder.NoFaktur} = " & txtOrder(0).Text
Report.DiscardSavedData
Report.RecordSelectionFormula = strTest

Report.Destination = crptToPrinter
Report.Action = 1

End Sub

Unfortunately I come up with these errors:
- Object variable or With block variable not set
on the line of:
Report.ReportFileName = "C:\RPT\Faktur PAS.rpt"

and:
- Invalid use of property
with the DiscardSavedData highlighted.

I am baffled here. Help, please?

Thank you so much. Note, the DiscardSavedData stuff works OK with CRViewer.

TavMaster-T
02-14-2007, 09:38 AM
After your declarations you need:
Set Report = New Crystal Report

That should clear up most of the errors.

I.m pretty sure the .RecordSelectionFormula property is read only. Use .SubreportToChange and .ReplaceSelectionFormula instead. First set .SubreportToChange to the name of the sub report or to "" for the main report. Then, use .ReplaceSelectionFormula to set the selection formula. (If you don't have any sub reports then you might not need to use the .SubreportToChange property but I'm not sure)

I've never used the .DiscardSavedData method in my code. I always turn of the "Save Data with Report" option.

Post again if you still have any problems

IndraG
02-15-2007, 06:31 PM
Hi again,
I am sorry for being such a pain, however, when I wrote:
Set Report = New Crystal Report
I got the error message: Compile Error, Expected: End of Message, when my cursor left the line.

When I change it to:
Set Report = New CrystalReport
I did not get any compile error when I left the line, but during run, it comes up with this error: Compile Error: Invalid use of new keyword

Help again, please?

TavMaster-T
02-16-2007, 08:22 AM
Sorry, CrystalReport is one word. I inadvertantly put a space in there when I typed it in.

Here's a sample routine. I tested it and it works for me. Try it out and see if you still get any errors.


Public Sub TestCRWReport(sReport As String, bPrintMode As Boolean)
Static crReport As CrystalReport 'Crystal Report Control
Dim sMsg As String

Set crReport = New CrystalReport 'initialize report object
crReport.ReportFileName = sReport

'set selection formula
crReport.SubreportToChange = ""
crReport.ReplaceSelectionFormula "<your selection formula here>'"

crReport.WindowState = crptMaximized
If bPrintMode Then
crReport.Destination = crptToPrinter
Else
crReport.Destination = crptToWindow
End If
crReport.WindowTitle = "SPC System Report"
crReport.Action = 1
End Sub

IndraG
02-16-2007, 04:55 PM
Hi again,
Now I am really baffled. I copy your code directly and adjust my code to fit it. It looks OK, when I press F5, it immediately highlight the New CrystalReport part of: Set crReport = New CrystalReport and come up with the error: Invalid use of new keyword!

The only difference that I can see in my program from your direction is the inclusion of the Crystal Report Control (Crystl32.OCX) in Components rather than References, since ocx are supposed to be in Components, right? Do I miss referencing anything else?

Thank you for being so patient with me. I really appreciate it.

TavMaster-T
02-20-2007, 09:48 AM
As far as I know, adding it in the Components section just adds it to your toolbox. The only reason to select the control in Components would be to allow adding the control to a form. I typically use it only in code (not on forms) so I typically only add under References and not under Components. Make sure you have it selected under References. It shouldn't hurt anything if it's also under Components, but it must be selected under References

IndraG
02-22-2007, 08:33 AM
Hi, TavMaster-T,
Sorry, I have been out of town for assignment, and only back now. I tried your suggestion immediately. When I called the ocx file into the reference list, it gives this warning:
Name conflicts with existing module, project, object library
even after I take it out of the component list (and I do not name anything close to that). The Crystal Report Control does not show up in the reference list, when I reopen it again. BUT, the difference is, it is working now! The printing works smoothly. I guess, there is a warning somehow for it to be called into reference, but it is still recognized, and work well.

Thank you so much for your patience. I really appreciate it. If I ever go through St Louis sometimes (sadly, the probability is really slim), I would look you up and at least buy you a meal or something, :) . Or, if you ever comes my way, in Indonesia, give me a call, and you got yourself a free tour guide, and free food and lodging, :) .

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum