printing crystal report records

how can I print a record from a crystal report.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MrBlubkeAuthor Commented:
please rather urgent....
Éric MoreauSenior .Net ConsultantCommented:
This is a procedure that will help you print CR report from VB.

You can specify a criteria. This will reduce the amount of data printed.

Public Sub PrintCRReport(ByVal pstrReportFile As String, _
                          Optional ByVal pstrCriteria As Variant, _
                          Optional ByVal pbytDestination As Byte = 0)
Dim intResult As Integer

    With fMainForm.CrystalReport1
        If Not IsMissing(pstrCriteria) Then
            .SelectionFormula = pstrCriteria
        End If

        .WindowState = crptMaximized
        .ReportFileName = App.path & "\" & pstrReportFile
        .Destination = pbytDestination
        .WindowTitle = App.Title & " - " & pstrReportFile
        intResult = .PrintReport
        If intResult <> 0 Then
            MsgBox "Error occured." & _
                   vbCrLf & .LastErrorNumber & ": " & .LastErrorString, _
                   vbCritical + vbOKOnly, _
                   "Print error"
        End If
    End With
End Sub
to call the actual file (eg Report.rpt) that was generated in VB all u have to do is

CrystalReport1.ReportFileName = "c:\report\filename.rpt"

so u can change that for different reports.... obviously the report must be configured to read the correct database and layout must be designed first.

to run the report in a preview window see the example code below having first putting the crystal report control on that form, and renaming it to CrystalReport

   Dim result As Integer
   CrystalReport.ReportFileName = c:\Reports\PatientWeekly.rpt"
' here would be crptToPrinter if u wanted to print it
   CrystalReport.Destination = crptToWindow
   result = CrystalReport.PrintReport
   If result <> 0 Then
      MsgBox "Print Error, Error Number = " & result
   End If

at the end if there is an error the user would be notified...

This is a simple code off the top of my head, it can be more complicated than that with more errorhandling routines

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.