 |
| Crystal Reports General discussion about Crystal Reports. For discussions specific to the book Professional Crystal Reports for VS.NET, please see the book discussion forum for that book. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Crystal Reports section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 28th, 2006, 08:59 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using Crystal in VB 2005 without Report Viewer
I am writing a Windows application with Visual Basic 2005. Within my application, I need to print a Crystal Report and pass it a few parameters. I have searched the online help, and it only shows how to do this with the Report Viewer. This is a step I need to bypass. I want to automate the printing process, and not show the user the report viewer, or even load it due to extra resources that I do not need.
I need to know how to pass parameters to the ReportDocument object programatically. So far, I have had no luck finding this anywhere online. I need your help!
Clif Dunaway
|
|

March 29th, 2006, 12:46 AM
|
|
Authorized User
|
|
Join Date: Sep 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Clif,
Try with below code
Public Sub PrintReport(Byval ReportName As CrystalDecisions.CrystalReports.Engine.ReportDocum ent, ByVal ParamName() As String, ByVal ParamValues() As String )
Dim MyReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocum ent = ReportName
Dim Field As CrystalDecisions.Shared.ParameterValues
Dim Value As CrystalDecisions.Shared.ParameterDiscreteValue
Try
If Not ParamName Is Nothing Then
For i As Integer = 0 To ParamName.Length - 1
Field = New CrystalDecisions.Shared.ParameterValues
Value = New CrystalDecisions.Shared.ParameterDiscreteValue
Value.Value = ParamValues(i)
Field.Add(Value)
MyReportDocument.DataDefinition.ParameterFields(Pa ramName(i)).ApplyCurrentValues(Field)
Next
End If
MyReportDocument.PrintOptions.PrinterName = ""
MyReportDocument.PrintToPrinter(1, True, 0, 0)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Print Report")
Finally
MyReportDocument.Close()
MyReportDocument.Dispose()
End Try
End Sub
example:
Private Sub PrintReport()
Dim strParamSelect As String
Dim strParamName() As String = {"xxx", "yyyy"}
Dim strParamValues() As String = {"Value of xxx", "Value of yyyy"}
PrintReport(New REPORTNAME, strParamName, strParamValues)
End Sub
|
|

March 29th, 2006, 09:41 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Awesome! Many thanks! I was able to get this working, with a tweak to load a report. To call the print routine I did this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strParamName() As String = {"@PalletID"}
Dim strParamValues() As String = {"J00002229401"}
Dim REPORTNAME As New CrystalDecisions.CrystalReports.Engine.ReportDocum ent
REPORTNAME.Load("C:\Reports\Report1.rpt")
PrintReport(REPORTNAME, strParamName, strParamValues)
End Sub
Clif Dunaway
|
|

April 3rd, 2006, 01:12 PM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I am now getting a connection error when I call my report to print. Crystal is saying:
Unable to connect: incorrect log on parameters.
I added this line:
MyReportDocument.SetDatabaseLogon("username", "password", "server", "database")
with real data, and it still has this error.
Does anyone know why this error is appearing? How can I get past this?
Clif Dunaway
|
|

April 3rd, 2006, 02:46 PM
|
|
Authorized User
|
|
Join Date: Sep 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Before loading the report you need to pass report databse login details .
|
|

April 4th, 2006, 01:09 AM
|
|
Authorized User
|
|
Join Date: Sep 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Clif,
Try with this below code...........
Private Sub LogOnReport(ByVal cr As CrystalDecisions.CrystalReports.Engine.ReportDocum ent)
Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo
For Each myTable In cr.Database.Tables
myLogin = myTable.LogOnInfo
myLogin.ConnectionInfo.ServerName ="ServerName"
myLogin.ConnectionInfo.DatabaseName = "DBNAme"
myLogin.ConnectionInfo.UserID = "USERNAME"
myLogin.ConnectionInfo.Password = "Password"
myTable.ApplyLogOnInfo(myLogin)
'Use this below line to set report location
If Not myTable.Name Like "Command*" Then myTable.Location = strInfo(1) & ".dbo." & myTable.Name
Next
' This below code is for subreport login
Dim RepObject As CrystalDecisions.CrystalReports.Engine.ReportObjec t
Dim SubRepDoc As CrystalDecisions.CrystalReports.Engine.SubreportOb ject
Dim RepDoc As CrystalDecisions.CrystalReports.Engine.ReportDocum ent
For Each RepObject In cr.ReportDefinition.ReportObjects
If RepObject.Kind = CrystalDecisions.Shared.ReportObjectKind.Subreport Object Then
SubRepDoc = CType(RepObject, CrystalDecisions.CrystalReports.Engine.SubreportOb ject)
RepDoc = cr.OpenSubreport(SubRepDoc.SubreportName)
For Each myTable In RepDoc.Database.Tables
myLogin = myTable.LogOnInfo
myLogin.ConnectionInfo.ServerName = strInfo(0)
myLogin.ConnectionInfo.DatabaseName = strInfo(1)
myLogin.ConnectionInfo.UserID = strInfo(2)
myLogin.ConnectionInfo.Password = strInfo(3)
myTable.ApplyLogOnInfo(myLogin)
If Not myTable.Name Like "Command*" Then myTable.Location = strInfo(1) & ".dbo." & myTable.Name
Next
End If
Next
End Sub
|
|

July 18th, 2007, 11:48 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have modified this to work for exporting to text and was wondering if anyone has had any luck in doing so as well. I cannot change the default export to text options. The characters per inch field needs to be changed from the default 12 to 16 and the Lines per page to 10.
I keep on getting errors when doing so and it just does not work for me. This is the code that I have modified from above.
Code:
Dim exportOpts As New ExportOptions
Dim diskOpts As DiskFileDestinationOptions
diskOpts = ExportOptions.CreateDiskFileDestinationOptions()
CrystalDecisions.Shared.ExportOptions.CreateTextFormatOptions.CharactersPerInch = 16
exportOpts.ExportFormatType = ExportFormatType.Text
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
diskOpts.DiskFileName = "c:\exporttotext.txt"
exportOpts.ExportDestinationOptions = diskOpts
ExportOptions.CreateTextFormatOptions.CharactersPerInch = 16 ' This line seems to do nothing but should do something
ExportOptions.CreateTextFormatOptions.LinesPerPage = 5 'Does nothing but should
MyReportDocument.Export(exportOpts)
Thanks for your help
|
|
 |