Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Export with record selection

I have a project that I have created with vb net 2005, I am using crystal report XI for the reports and using the viewer inside the project to view the reports. The Problem I'm having is when I go to auto export the data to a file. It exports the data but does not hold the record selection all the time. Sometimes the record selection will work sometimes it will not. Any one have any idea what is going on? I have included my code for the report and export.

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

Imports CrystalDecisions.ReportSource

Imports CrystalDecisions.Windows

Imports System.IO

Imports System.Windows.Forms

Imports System.Data.SqlClient

Imports System.Data

Public Class report

Inherits System.Windows.Forms.Form

Dim cryRpt As New ReportDocument

Dim crReport As New ReportDocument()

Dim mySelectFormula As String = Formula

Dim crConnectionInfo As ConnectionInfo = New ConnectionInfo()

Private Sub CrystalReportViewer1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Public Sub New()

RptName = "
dell2900-envoy\Share_D\env10rpts" & "\" & RptName

' This call is required by the Windows Form Designer.

InitializeComponent()

Dim crConnectionInfo As New ConnectionInfo

Dim crtableLogoninfos As New TableLogOnInfos

Dim crtableLogoninfo As New TableLogOnInfo

Dim CrTables As Tables

Dim CrTable As Table

' LOADING REPORT FIRST

cryRpt.Load(RptName)

With crConnectionInfo

.ServerName = "server"

.DatabaseName = "database"

.UserID = "user"

.Password = "password"

End With

CrTables = cryRpt.Database.Tables

For Each CrTable In CrTables

crtableLogoninfo = CrTable.LogOnInfo

crtableLogoninfo.ConnectionInfo = crConnectionInfo

CrTable.ApplyLogOnInfo(crtableLogoninfo)

Next

'sdl this is stuff added for subreports

'MOVED THIS TO BEFORE VIEWING REPORT

If cryRpt.RecordSelectionFormula = "" Then

CrystalReportViewer1.SelectionFormula = mySelectFormula

CrystalReportViewer1.ViewTimeSelectionFormula = mySelectFormula

Else

CrystalReportViewer1.SelectionFormula = cryRpt.RecordSelectionFormula & " and " & mySelectFormula

CrystalReportViewer1.ViewTimeSelectionFormula = cryRpt.RecordSelectionFormula & " and " & mySelectFormula

End If

CrystalReportViewer1.ReportSource = cryRpt

End Sub

Public Sub exportfile()

' Dim crReport As New ReportDocument

Dim crDiskFileDestinationOptions As DiskFileDestinationOptions

Dim csvExportOptions As New CharacterSeparatedValuesFormatOptions

Dim ExportPath As String

'Setup the export

If wsExportOnly = True Then

wsExportOnly = False

ExportPath = "d:\"

If Directory.Exists(ExportPath) = False Then

Directory.CreateDirectory(ExportPath)

End If

crDiskFileDestinationOptions = New DiskFileDestinationOptions()

crDiskFileDestinationOptions.DiskFileName = ExportPath + "invexprt.txt"

cryRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile

cryRpt.ExportOptions.ExportFormatType = ExportFormatType.Text

cryRpt.ExportOptions.DestinationOptions = crDiskFileDestinationOptions

End If

If wsTrendExport = "Yes" Then

wsTrendExport = "No"

ExportPath = "
Dell6600\Share_G\Trend\"

'ExportPath = "d:\"

If Directory.Exists(ExportPath) = False Then

Directory.CreateDirectory(ExportPath)

End If

crDiskFileDestinationOptions = New DiskFileDestinationOptions()

crDiskFileDestinationOptions.DiskFileName = ExportPath + "trendemp.csv"

cryRpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile

cryRpt.ExportOptions.ExportFormatType = ExportFormatType.CharacterSeparatedValues

csvExportOptions.ExportMode = CsvExportMode.Legacy

cryRpt.ExportOptions.DestinationOptions = crDiskFileDestinationOptions

End If

Try

' Export the report

cryRpt.Export()

MsgBox("Report exported successfully.")

'crReport.Close()

'crReport.Dispose()

'RptName = ""

'formula = ""

'tmpFormula = ""

Catch err As Exception

MsgBox(err.Message.ToString)

End Try

end if

End Sub

Private Sub report_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

crReport.Close()

crReport.Dispose()

RptName = ""

Formula = ""

tmpFormula = ""

frmReports.Show()

End Sub

replied

The only place I see you setting a record selection formula is on the viewer object. This will not be applied if you export through code. To do so you would have to apply the record selection formula to the report object directly. The ReportDocument object has a RecordSelectionFormula property that you can use.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question