Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to run Crystal Report From within VB program 1

Status
Not open for further replies.

tyedyejenn

Programmer
Jun 14, 2000
35
US
I am writing a program in Visual Basic 6 and need to print a report.&nbsp;&nbsp;I designed the report in Crystal Report 8.&nbsp;&nbsp;How do I get the report to print from my program?&nbsp;&nbsp;Also, do the records have to come from a database or can the records come from records stored in arrays within program?<br>Thanks in advance for the help.<br>Jenn
 
There is more than one way to achieve this. The best way is to use the Report Designer Component (RDC). This comes with the developer version of the program. The other(more difficult) way is to use the Crystal Automation object. If you want to pass data to a report that is in an array or only generated at runtime then you need to use the Crystal Data Object (CDO). <br><br>There is extensive help available within the program on how to use these methods.
 
The first post is correct and there are many ways of doing it. First of all the fields in your report could be bound to data from a database, or they can be formula fields that are filled from a query, which means yes they could be stored in array which populates at runtime. Also there are techniques for using grids as the source of data.

Here are a couple of simplistic samples that may give you a hint or two for simple reports.

This example has the fields bound to a database inside of the RPT file in the Crystal Reports designer. Then at runtime you can point to any database with the same structure. Various properties are arbitrarily set in this example.

'Set properties of the report viewing window
'(crpt1 is the name of the Crystal Reports Control
'on the form)

With frmMainMenu.crpt1
.WindowShowGroupTree = False 'Prevents the Tree from being shown
.WindowAllowDrillDown = False 'Prevents Drilldown
.WindowState = crptToWindow 'send report to window
.WindowControls = True 'activates windows controls
.WindowState = crptMaximized 'maximize window
.WindowBorderStyle = 2 'Sizable Window
.WindowControlBox = True 'Activates control box
.WindowMaxButton = True 'Turns on Maximize button
.WindowMinButton = True 'Turns on Minimize button
.PageZoom (2) 'Enables Zoom Display
.WindowTitle = &quot;Acme Enterprises&quot;
'If the report is a drilldown set the tree view on and the Drilldown feature
If intrType = 1 Then
.WindowShowGroupTree = True
.WindowAllowDrillDown = True
End If

.DataFiles(0) = strPath 'set datapath to RIM
.ReportFileName = strpath & strfilename
'Reportname and path
.CopiesToPrinter = 1 'Default to printer
.Action = 1 'run the report
End With

This example sets formula fields in Crystal Reports at run time from the data in an array. I left the array part out but once it has been populated here is how it looks.

'Set properties of the report viewing window
With frmImpProjDetail.CrystalReport1
.WindowShowGroupTree = False 'Prevents the Tree from being shown
.WindowAllowDrillDown = False 'Prevents Drilldown
.WindowState = crptToWindow 'send report to window
.WindowControls = True 'activates windows controls
.WindowState = crptMaximized 'maximize window
.WindowBorderStyle = 2 'Sizable Window
.WindowControlBox = True 'Activates control box
.WindowMaxButton = True 'Turns on Maximize button
.WindowMinButton = True 'Turns on Minimize button
.PageZoom (2) 'Enables Zoom Display
.WindowTitle = &quot;Acme Enterprises&quot;
.DataFiles(0) = strDataPath 'set datapath to RIM
.CopiesToPrinter = 1 'Default to printer

'Set the values to be placed into the report fields
.Formulas(0) = &quot;Field1 = '&quot; & strWordArray(0) & &quot; '&quot;
.Formulas(1) = &quot;Field2 ='&quot; & strWordArray(1) & &quot; '&quot;
.Formulas(2) = &quot;Field3 ='&quot; & strWordArray(2) & &quot; '&quot;
.Formulas(3) = &quot;Field4 ='&quot; & strWordArray(3) & &quot; '&quot;
.Formulas(4) = &quot;Field5 ='&quot; & strWordArray(4) & &quot; '&quot;
.Formulas(5) = &quot;Field6 ='&quot; & strWordArray(5) & &quot; '&quot;
.Formulas(6) = &quot;Field7 ='&quot; & strWordArray(6) & &quot; '&quot;
.Formulas(7) = &quot;field8 ='&quot; & strWordArray(7) & &quot; '&quot;

.Action = 1
End With

 
Anyone running crystal from VBA?'
If so how, I think I have the runtime and viewer references called out, yet cannot seem to recognise the sub openreport.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top