以后你会发现我使用的过程(我简化了它,抑制了我们自己的对象和全局变量)。此过程允许将报表从开发时使用的原始连接重定向到活动 SQL 服务器。它是用 VB 编写的,使用 2 个主要对象:
- 通过水晶报表实例打开的原始报表对象
- ADODB 连接是当前 SQL 服务器的活动连接(称为 P_currentConnection)
在应用程序中查看/打印报表对象之前调用此函数(也可以是子函数)。它可以用于在复制数据库之间分发报告时,用户根据他们的位置连接到不同的服务器/数据库。
Public Function connectReportToDatabase( _
P_report As CRAXDRT.Report)
Dim table As CRAXDRT.DatabaseTable, _
For Each table In P_report.Database.tables
If table.DllName <> "crdb_ado.dll" Then
table.DllName = "crdb_ado.dll"
End If
table.ConnectionProperties.DeleteAll
table.ConnectionProperties.Add "Provider", P_currentConnection.Provider
table.ConnectionProperties.Add "Data source", P_currentConnection.Properties("Data source").Value
table.ConnectionProperties.Add "Database", P_currentConnection.DefaultDatabase
table.ConnectionProperties.Add "Integrated security", P_currentConnection.Properties("Integrated security").Value
table.ConnectionProperties.Add "Persist Security Info", P_currentConnection.Properties("Persist Security Info").Value
table.ConnectionProperties.Add "Initial Catalog", P_currentConnection.Properties("Initial Catalog").Value
table.SetTableLocation table.location, "", P_currentConnection.ConnectionString
table.TestConnectivity
Next table
可以使用以下过程调用它:
Dim crystal As CRAXDRT.Application, _
m_report as CRAXDRT.report
Set crystal = New CRAXDRT.Application
Set m_rapport = crystal.OpenReport(nameOfTheReport & ".rpt")
connectreportToDatabase(m_report)
如果您的报告包含子报告,您可能还必须将它们重定向到活动连接。在这种情况下,您必须浏览报表中的所有对象,检查属于报表类型的对象并将它们重定向到新连接。我相信你会很高兴在这个原始过程中添加相应的额外行。