VB6 and crystal reports 8.5

need help with these 2 working together

Page 1 of 1

2 Replies - 1800 Views - Last Post: 08 April 2009 - 06:12 AM Rate Topic: -----

#1 kajirus3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-August 08

VB6 and crystal reports 8.5

Post icon  Posted 31 March 2009 - 10:23 AM

I have a report named control_record.rpt that does everything i want in crystal reports 8.5. It prints all the records.
What i cant figure out is how to call the report from within vb6 when a button is clicked. ANyone help with this?


Thanks in Advacne

This post has been edited by kajirus3: 31 March 2009 - 10:24 AM

Is This A Good Question/Topic? 0
  • +

Replies To: VB6 and crystal reports 8.5

#2 emacrack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 28-March 09

Re: VB6 and crystal reports 8.5

Posted 01 April 2009 - 10:00 AM

Well i use crystal reports XI R2 but it's the same process to link the .rpt file with the vb6 aplication. I don't remember if in the 8.5 version is necessary, but in the XI R2 you need to have a "developer" license to use it trought visual basic.
There's two ways to do what you want.

- The first one is disigning the report directly trought visual basic, adding the Crystal Reports Designer. I will not explain this method since you are telling that you already have the report disigned in Crystal Report with the corresponding .rpt file. Anyway, this method is for making simple reports since it doesn't bring you as much disign tools as the Crystal Reports aplication.

- The second way (and the better one in my opinion) is designing the report first with the Crystal Reports aplication, and saving the file. Then in Visual Basic you need to add the "Crystal Reports 9 ActiveX Designer Run Time Library" reference and the "Crystal Reports Viewer Control" component.
Add the CRViewer object (used to show the .rpt report file) to a form and use this code:

Variable Declaration:
Option Explicit
Private crApp As New CRAXDRT.Application
Private crReport As New CRAXDRT.Report


Code in the FormLoad event:
 Private Sub Form_Load()

Dim crParamDefs As CRAXDRT.ParameterFieldDefinitions
Dim crParamDef As CRAXDRT.ParameterFieldDefinition

On Error GoTo ErrHandler

	'Opening the report
	Screen.MousePointer = vbHourglass
	Set crReport = crApp.OpenReport("ReportFilePath.rpt", 1)

	' If you want to send parameters to it
	Set crParamDefs = crReport.ParameterFields
	For Each crParamDef In crParamDefs
		Select Case crParamDef.ParameterFieldName
			Case "Parametro1"
				 crParamDef.AddCurrentValue (mstrParametro1)
			Case "Parametro2"
				 crParamDef.AddCurrentValue (mlngParametro2)
		End Select
	Next

	crViewer.ReportSource = crReport
	crViewer.DisplayGroupTree = False
	crViewer.ViewReport
	Screen.MousePointer = vbDefault
	
	Set crParamDefs = Nothing
	Set crParamDef = Nothing
	Exit Sub

ErrHandler:
	If Err.Number = -2147206461 Then
		MsgBox "The file doesn't exist", vbCritical + vbOKOnly
	Else
		MsgBox Err.Description, vbCritical + vbOKOnly
	End If
	Screen.MousePointer = vbDefault

End Sub


Unload Cleaning
 Private Sub Form_Unload(Cancel As Integer)
	Set crReport = Nothing
	Set crApp = Nothing
End Sub


So, when you want to show the report by clicking a button, you only have to show this form and that's all.
Note that in the FormLoad event we are opening and sending the data to the .rpt file previously created. In this case i'm sending only parameters type of data, but you can add any other.
Well i hope that this help you at least to begin. Greetings... ;)

This post has been edited by emacrack: 01 April 2009 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 scottkellman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-March 09

Re: VB6 and crystal reports 8.5

Posted 08 April 2009 - 06:12 AM

View Postkajirus3, on 31 Mar, 2009 - 09:23 AM, said:

I have a report named control_record.rpt that does everything i want in crystal reports 8.5. It prints all the records.
What i cant figure out is how to call the report from within vb6 when a button is clicked. ANyone help with this?


Thanks in Advacne


I am passing a custom SQL query as well as just passing parameters will not always get the job done

 Dim sql As String
	Dim yBeginDate As String
	Dim yEndDate As String
			
	'clear out old data
	Me.CrystalReport1.DiscardSavedData = True
	Me.CrystalReport1.Reset
 
	'get the start and stop date for the report
	GetDates Trim(Str(Me.cboMonth.ListIndex + 1)), Trim(Me.cboYear.Text)
		
	sql = "({AuditTable.AuditDate} >= DateTime("
	sql = sql + Str(Year(dtStartDate))
	sql = sql + "," & Month(dtStartDate)
	sql = sql + "," & Day(dtStartDate)
	sql = sql + "," & Hour(dtStartDate)
	sql = sql + "," & Minute(dtStartDate)
	sql = sql + "," & Second(dtStartDate) & "))"
	sql = sql + " AND ({AuditTable.AuditDate} <= DateTime("
	sql = sql + Str(Year(dtEndDate))
	sql = sql + "," & Month(dtEndDate)
	sql = sql + "," & Day(dtEndDate)
	sql = sql + "," & Hour(dtEndDate)
	sql = sql + "," & Minute(dtEndDate)
	sql = sql + "," & Second(dtEndDate) & "))"
	
	'reset all good params
	Me.CrystalReport1.SelectionFormula = sql
	Me.CrystalReport1.ReportFileName = "report name.rpt"
	Me.CrystalReport1.Connect = "uid=xxxx;pwd=xxxx;"
	Me.CrystalReport1.PrintFileType = crptRecord
	Me.CrystalReport1.ReportSource = crptReport
	Me.CrystalReport1.ReportTitle = "Audit Report - Deleted Entries"
	Me.CrystalReport1.WindowTitle = "Reports: Audit Report - Deleted Entries"
	
	yBeginDate = "DateTime(" & Year(dtStartDate) & "," & Month(dtStartDate) & "," & Day(dtStartDate) & "," & Hour(dtStartDate) & "," & Minute(dtStartDate) & "," & Second(dtStartDate) & ")"
	yEndDate = "DateTime(" & Year(dtEndDate) & "," & Month(dtEndDate) & "," & Day(dtEndDate) & "," & Hour(dtEndDate) & "," & Minute(dtEndDate) & "," & Second(dtEndDate) & ")"
	
	Me.CrystalReport1.Formulas(0) = "startDate = " & yBeginDate
	Me.CrystalReport1.Formulas(1) = "endDate = " & yEndDate
			
	'launch the report
	Me.CrystalReport1.WindowState = crptMaximized
	Me.CrystalReport1.Action = 1


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1