'***************************************************************************************************************
'
' Name: CrystalReports
'
' Purpose: Allows easy setup of the report object for use in the Crystal reports
viewer
'
'***************************************************************************************************************
Option Explicit
Private Report As CRAXDRT.Report
Private app As New CRAXDRT.Application
' Store all databases and servers for main report and subreports for easy modification
Private Type ReportConnections
DatabaseName As String
ServerName As String
SubReport As CRAXDRT.Report
TableNumber As Integer
End Type
Private numConnections As Integer
Private connections(100) As ReportConnections
Enum CrystalValueTypes
crDate = 1
crNumber
crText
End Enum
'***********************************************************************************************
' Properties
'***********************************************************************************************
Property Get ConnectionCount() As Integer
' Returns the number of connections for main report and subreports
ConnectionCount = numConnections
End Property
Property Get CrystalDate() As Integer
CrystalDate = crDate
End Property
Property Get CrystalNumber() As Integer
CrystalNumber = crNumber
End Property
Property Get CrystalText() As Integer
CrystalText = crText
End Property
Property Get DatabaseName(n As Integer) As String
' Returns the database name for the selected connection
' For the main report, also applies to the table name
If n > 0 And n <= numConnections Then
DatabaseName = connections(n).DatabaseName
Else
DatabaseName = ""
End If
End Property
Property Get ParameterCount() As Integer
ParameterCount = Report.ParameterFields.count
End Property
Property Get ParameterDefault(n As Integer) As String
ParameterDefault = ""
If n > 0 And n <= Report.ParameterFields.count Then
If Report.ParameterFields.Item(n).IsDefaultValueSet Then _
ParameterDefault = Report.ParameterFields.Item(n).GetNthDefaultValue(1)
End If
End Property
Property Get ParameterName(n As Integer) As String
If n > 0 And n <= Report.ParameterFields.count Then
ParameterName = Report.ParameterFields.Item(n).ParameterFieldName
Else
ParameterName = ""
End If
End Property
Property Get ParameterPrompt(n As Integer) As String
If n > 0 And n <= Report.ParameterFields.count Then
ParameterPrompt = Report.ParameterFields.Item(n).prompt
Else
ParameterPrompt = ""
End If
End Property
Property Get ParameterSize(n As Integer) As Integer
If n > 0 And n <= Report.ParameterFields.count Then
ParameterSize = Report.ParameterFields.Item(n).NumberOfBytes
Else
ParameterSize = 0
End If
End Property
Property Get ParameterType(n As Integer) As Integer
If n > 0 And n <= Report.ParameterFields.count Then
Select Case Report.ParameterFields.Item(n).ValueType
Case crDateField, crDateTimeField
ParameterType = CrystalDate
Case crNumberField, crCurrencyField
ParameterType = CrystalNumber
Case crStringField
ParameterType = CrystalText
End Select
Else
ParameterType = -1
End If
End Property
Property Get ReportObj() As CRAXDRT.Report
Set ReportObj = Report
End Property
Property Get ServerName(n As Integer) As String
' Returns the server name for the selected connection
' For the main report, also applies to the table name
If n > 0 And n <= numConnections Then
ServerName = connections(n).ServerName
Else
ServerName = ""
End If
End Property
Property Let SetParameterValue(n As Integer, v As Variant)
If n > 0 And n <= Report.ParameterFields.count Then
If Not Report.ParameterFields.Item(1).EnableMultipleValues Then _
Report.ParameterFields.Item(n).ClearCurrentValueAndRange
If ParameterType(n) = CrystalDate Then
' Note: date parameters must be passed as a date value
Report.ParameterFields.Item(n).AddCurrentValue CDate(v)
Else
Report.ParameterFields.Item(n).AddCurrentValue v
End If
End If
End Property
Property Get TableCount() As Integer
' Returns table count for main report only
TableCount = Report.Database.tables.count
End Property
Property Get TableName(n As Integer) As String
' Returns the table name for the selected table - for main report only
If n > 0 And n <= Report.Database.tables.count Then
TableName = Report.Database.tables(n).Name
Else
TableName = ""
End If
End Property
'***********************************************************************************************
' Public routines
'***********************************************************************************************
Public Function OpenReport(reportName As String) As Integer
OpenReport = 0
On Error Resume Next
Set Report = app.OpenReport(reportName)
If Err.Number <> 0 Then OpenReport = -1
GetConnections
End Function
Public Sub SetLoginInfo(n As Integer, ServerName As String, Optional DatabaseName
As String = "")
' Changes the server and optionally the database
' Not using user login - assumes report is set up for a trusted connection
Dim whichReport As CRAXDRT.Report
If n > 0 And n <= numConnections Then
' Get the main or subreport as necessary
If n <= Report.Database.tables.count Then
Set whichReport = Report
Else
Set whichReport = connections(n).SubReport
End If
If DatabaseName <> "" Then
' Change both server and database names
whichReport.Database.tables(connections(n).TableNumber).SetLogOnInfo ServerName,
DatabaseName
connections(n).DatabaseName = DatabaseName
Else
' Only change server name
whichReport.Database.tables(connections(n).TableNumber).SetLogOnInfo ServerName
End If
connections(n).ServerName = ServerName
End If
End Sub
Private Sub GetConnections()
' Gets all the database and server names for the main report and all sub reports
Dim CRXSection As CRAXDRT.Section
Dim CRXSubreport As CRAXDRT.Report
Dim n As Integer
Dim reportObject As Object
' First get the main report
numConnections = 0
For n = 1 To Report.Database.tables.count
numConnections = numConnections + 1
connections(numConnections).ServerName = Report.Database.tables(n).LogOnServerName
connections(numConnections).DatabaseName = Report.Database.tables(n).LogOnDatabaseName
connections(numConnections).TableNumber = n
Next
' Now get the subreports
For Each CRXSection In Report.Sections
For Each reportObject In CRXSection.ReportObjects
If reportObject.Kind = crSubreportObject Then
Set CRXSubreport = reportObject.OpenSubreport
For n = 1 To CRXSubreport.Database.tables.count
numConnections = numConnections + 1
Set connections(numConnections).SubReport = CRXSubreport
connections(numConnections).ServerName = CRXSubreport.Database.tables(n).LogOnServerName
connections(numConnections).DatabaseName = CRXSubreport.Database.tables(n).LogOnDatabaseName
connections(numConnections).TableNumber = n
Next
End If
Next
Next
End Sub