Tuesday, February 14, 2012

CrystalReports in VB6

Hello,

I want to display a Crystalreport in VB6.
When i develop a Crystalreport a form is added with the following ocde:

Dim Report As New CrystalReport1

Private Sub Form_Load()
Screen.MousePointer = vbHourglass
CRViewer1.ReportSource = Report
CRViewer1.ViewReport
Screen.MousePointer = vbDefault

End Sub

Private Sub Form_Resize()
CRViewer1.Top = 0
CRViewer1.Left = 0
CRViewer1.Height = ScaleHeight
CRViewer1.Width = ScaleWidth

There is a parameter which should be sended to this report in order to collect to correct recordset from our SQLserver.
The Fieldname is CrystalReport1.Field1

Does anyone know how to do this?

Hope to hear from anyone.

Thanks in advance,
XanderPublic Sub CreateNewViewerWithParametersForDateAndDept(SelectionFormula As String, ReportPath As String, sStartDate As String, sStopDate As String, sDept As String, SingleDate As Boolean)

On Error GoTo CreateNewViewerError


Dim miReport As CRAXDDRT.Report
Dim miCRApp As New CRAXDDRT.Application
Dim DBTable As CRAXDRT.DatabaseTable
Set miReport = miCRApp.OpenReport(ReportPath)
Dim sDataBasePath As String

sDataBasePath = App.Path & "\Commercial.mdb"
Dim ConnectionInfo As CRAXDRT.ConnectionProperties
'Set the Connection Info to Connection Properties of the table object
Set ConnectionInfo = miReport.Database.Tables(1).ConnectionProperties
'Clear the ConnectionProperties collection.
ConnectionInfo.DeleteAll
'Add the database used by the report
ConnectionInfo.Add "Database Name", sDataBasePath
Set DBTable = miReport.Database.Tables(1)
DBTable.Location = sDataBasePath
With miReport
If (Len(SelectionFormula) > 0) Then
.RecordSelectionFormula = SelectionFormula
End If
If Not (SingleDate) Then
.ParameterFields(2).AddCurrentRange CDate(sStartDate), CDate(sStopDate), crRangeIncludeLowerBound + crRangeIncludeUpperBound
Else
.ParameterFields(2).AddCurrentValue CDate(sStartDate)
End If
.ParameterFields(4).AddCurrentValue (sDept)
If Not (ReportTitle = Empty) Then
.Sections("PH").ReportObjects("SummaryOfPayPeriod").SetText ReportTitle
End If
.Sections("PH").ReportObjects("Department").SetText "Dept." & sDept
End With
With ENG.crvCommercial
.ReportSource = miReport
.ViewReport
.DisplayGroupTree = False
.EnableProgressControl = True
While .IsBusy
DoEvents
Wend
.Zoom "100"
.Visible = True
End With
Set miCRApp = Nothing
Set miReport = Nothing
ENG.chkIndiviual.Value = 0 'Make sure that the Print command is cleared
ENG.MousePointer = vbDefault
Exit Sub
CreateNewViewerError:
MsgBox Err.Description, vbInformation, "Error"
Set miCRApp = Nothing
Set miReport = Nothing
ENG.chkIndiviual.Value = 0 'Make sure that the Print command is cleared
ENG.MousePointer = vbDefault

End Sub

LEGEND:
CreateNew Viewer With Parameters For Date And Dept
SelectionFormula As String = Created Selection formula.
ReportPath As String = Where and what report you want to use.
sStartDate As String = Start Date of selection.
sStopDate As String = Ending Date of selection
sDept As String = What Department
SingleDate As Boolean = Is only a single date required.

I use this function to create a viewer for my reports based on certain paramters that are selected by the user. This function is a part of a program that I wrote for the company that I work for. It will track TimeSheets for technicians and create reports based on the data. This program is in VB6 SP6 and Crystal 9.|||This should work:

Report.ParameterFields(1).AddCurrentValue (Value)

the number between () is the index (first one is 1, second 2,....)
in "Value" you have to put the value of the parameter field.

No comments:

Post a Comment