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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export Crystal Report using VB6

Status
Not open for further replies.

tenbobmillionaire

Programmer
Jun 13, 2000
27
EU
Does anyone have any demo code on how to export a crystal report using VB? I need to allow a user to enter a field in a text box in a VB form, then pass this parameter to crystal (version 6) , generate the report using this parameter, export to html and email the user with the html file as an attachment. Simple as that.(?)

I've never used crystal with VB before so any help is greatly appreciated.

Mick
 
Hi Mick,

I know it has been a long time since you wrote this, but did you ever work out how to do this? I need to do exactly the same except export to PDF rather than html.

Thanks

Kate
 
Hi Kate
Yes its really quite easy to run crystal through vb, - I use the crystl32.ocx object, you can send anything from vb to crystal eg selection criteria, export format, whatever.
eg:
CrystalReport1.ReportFileName = "c:\test.rpt
CrystalReport1.Destination = crptToFile
CrystalReport1.SelectionFormula ="{FieldID} =" & text1.text
CrystalReport1.PrintFileType = crptHTML30
CrystalReport1.Action = 1

Not sure about exporting to pdf - think you need crystal 8 for that, I only have version six on this pc.
If you need a demo form I can send you it.
Hope this helps - how long do they keep stuff in this tek tips database anyway...?
Mick
 
Thanks Mick,

I have Crystal 8 so that is no problem, if you could send the demo form, that would be fantastic - kate_b17@hotmail.com

Thanks so much for your help on this!

BTW, I was reading a thread from 1999 so I think they keep these for quite a while!

Kate
 
You can quite easily export / pass parameters to crystal reports and export to any format from VB. To export to PDF you will require Crystal 8.5. The following code is an example. It takes an XML stream and merges the data with a crystal report. Formula fields on the report are concidered unique XML nodes.

Parameters can also be passed using 'Report.ParameterFields'.

This code also constructs ADO recordsets at runtime and populates them from the XML data.

You will need to add a reference to Crystal Reports 8.5 ActiveX design and runtime library.

Hope this is usefull.

Chris Dukes



Dim objformula As CRAXDRT.FormulaFieldDefinition
Dim objtables As DatabaseTable
Dim objfield As DatabaseFieldDefinition
Dim objADORecords As ADODB.Recordset
Dim StrTablename As String
Dim objNodes As MSXML2.IXMLDOMNodeList
Dim objelement As MSXML2.IXMLDOMElement
Dim i, j As Integer
Dim strFileName As String
Dim strUniqueReference As String
Dim mobjParam As CRAXDRT.ParameterFieldDefinition



If mobjcrystal Is Nothing Then
Set mobjcrystal = New CRAXDRT.Application
End If

Set mobjReport = mobjcrystal.OpenReport(Me.ReportFileName)

' Loop round for all the formula in the
' report and get the text from the
' XML document based on the name of the
' Crystal formula field
For Each objformula In mobjReport.FormulaFields

' If the formula field already has text in it
' do not overright the current text as the
' report could be using the formula
If Len(Trim(objformula.Text)) = 0 Then
objformula.Text = objXMLData.GetTextElement( _
objformula.FormulaFieldName, CrystalFormat)
End If

Next objformula


If mobjReport.ParameterFields.Count <> 0 Then
Set mobjParam = mobjReport.ParameterFields.GetItemByName( _
PARAMETER_TYPE)

mobjParam.AddCurrentValue (iReportType)
End If

' now loop around the main report
' tables and add the
' data sources
For Each objtables In mobjReport.Database.Tables

StrTablename = Left(objtables.Name, Len(objtables.Name) - 4)
Set objADORecords = objXMLData.GetRecordSet(StrTablename)

If objADORecords Is Nothing Then

For Each objfield In objtables.Fields

Call objXMLData.GenerateRecordSetFromCrystal( _
objADORecords, StrTablename, objfield.DatabaseFieldName, _
objfield.ValueType)

Next objfield

End If

If objADORecords.State <> 1 Then
objADORecords.Open
End If

Set objNodes = objXMLData.GetNodeList(StrTablename)

For i = 0 To objNodes.length - 1
Set objelement = objNodes.Item(i)

objADORecords.AddNew

If objelement.childNodes.length = 1 Then
objADORecords.Fields( _
objelement.baseName).Value = FormatValue( _
objelement.Text, _
objADORecords.Fields(objelement.baseName).Type)
Else
For j = 0 To objelement.childNodes.length - 1
objADORecords.Fields(objelement.childNodes( _
j).baseName).Value = FormatValue( _
objelement.childNodes(j).Text, _
objADORecords.Fields(objelement.childNodes( _
j).baseName).Type)
Next j
End If

objADORecords.Update
Next i

objtables.SetDataSource objADORecords, 3

Next objtables


' Process the report
mobjReport.ReadRecords

Set mobjExport = mobjReport.ExportOptions

' Export the file to PDF format
mobjExport.PDFExportAllPages = True
mobjExport.DestinationType = crEDTDiskFile
mobjExport.DiskFileName = strFilePath & &quot;\&quot; & strFileName & &quot;.pdf&quot;
mobjExport.FormatType = crEFTPortableDocFormat


mobjReport.Export False



 
Hi,all,

May I ask you a simple question: how to export the crystal report to PDF file from VB? Now I just know how to view a CR from VB like:
CRep1.Destination = 0
CRep1.ReportFileName = strpath
CRep1.Action = 1
Could you please give me some codes on how to export the CR to PDF file? and the following code and method doesn't work for me, I don't know why.
Set mobjExport = mobjReport.ExportOptions
BTW, what does &quot;add a reference to Crystal Reports 8.5 ActiveX design and runtime library&quot; mean?
Thank you so much!

Regards,
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top