This is sample code how to using Crystal Report 8 ActiveX with Visual Basic 6.
It include 2 functions: to view on screen and print to printer.


'Code:
Dim CRReport As CRPEAuto.Report
Dim CRWindow As CRPEAuto.Window
Dim CRView As CRPEAuto.View
Dim CRApp As CRPEAuto.Application
Public ReportFileName As String
Public Title As String

Public Sub Preview()
On Error Goto ErrMsg
Set CRApp = CreateObject("crystal.crpe.application")
Set CRReport = CRApp.OpenReport(App.Path + "\report\" + ReportFileName)
CRReport.DiscardSavedData
Set CRView = CRReport.Preview(Title)
Set CRWindow = CRView.Parent
CRWindow.ControlsVisible = True
Exit Sub
ErrMsg:
MsgBox CStr(Err.Number) + " : " + Err.Description
End Sub

Public Sub PrintToPrinter()
On Error Goto ErrMsg
Set CRApp = CreateObject("crystal.crpe.application")
Set CRReport = CRApp.OpenReport(App.Path + "\report\" + ReportFileName)
CRReport.DiscardSavedData 'reset all the data In report
CRReport.PrintOut
Exit Sub
'-------------end code


Crystal Report 8 with VB6 to enhanced with RecordSelectionFormula.
Used to view and print crystal report 8 and allow user to filter the report using RecordSelectionFormula function.

'code:
Dim CRReport As CRPEAuto.Report
Dim CRWindow As CRPEAuto.Window
Dim CRView As CRPEAuto.View
Dim CRApp As CRPEAuto.Application
Public ReportFileName As String
Public Title As String


Public Sub Preview()
On Error Goto ErrMsg
Set CRApp = CreateObject("crystal.crpe.application")
Set CRReport = CRApp.OpenReport(App.Path + "\report\" + ReportFileName)
CRReport.DiscardSavedData
CRReport.RecordSelectionFormula = "{RptItemDetail.qty} > 1" 'put ur own String here
Set CRView = CRReport.Preview(Title)
Set CRWindow = CRView.Parent
CRWindow.ControlsVisible = True
Exit Sub
ErrMsg:
MsgBox CStr(Err.Number) + " : " + Err.Description
End Sub
'--------end code


Crystal Report 8 with VB6 to Print a crystal report in a certain date range

'code:
report.ReportFileName = gvPath & "\reportname.rpt"
report.CopiesToPrinter = InputBox("How many copies would you like to print:")
report.SelectionFormula = "{cheques.date} In Date (" & Format$(Startdatetextbox.Value, "dd,mm,yyyy") & ") To Date (" & Format$(enddatetextbox.Value, "dd,mm,yyy") & ")"
report.ReportTitle = "Report between" & " " & Format$(Startdatetextbox.Value, "long date") & " " & "and" & " " & Format(enddatetextbox.Value, "long date")
report.Action = 1
'-------------end code

1 komentar:

John Ween said... 4/08/2011

Great Post. This is new to me and I appreciate it.
Thanks!

 
Top