我想知道如何使用 Visual Basic 6 为 Crystal Reports 8.5 创建连接字符串语句。我尝试过以下代码:
CrystalReport1.ReportFileName = "C:\Report1.rpt"
CrystalReport1.Destination = crptToWindow
CrystalReport1.DiscardSavedData = True
CrystalReport1.Connect ="Data Source=Localhost;UID=sa;PWD=****;DSQ=Dat BdName;"
CrystalReport1.WindowState = crptMaximized
CrystalReport1.Action = 1
但我无法理解第四行。有人可以向我解释一下吗?我使用 MS Access 2013 作为数据库。 任何帮助将不胜感激。
经过一些修改,这应该可以工作:
Public Sub OpenReport(ReportPath As String, DataPath As String)
' 1) add a reference to the Crystal Reports 8.5 ActiveX Designer Run Time Library
' 2) place a CrystalActiveXReportViewer control named crView to your form
Dim oCRapp As CRAXDRT.Application
Dim oReport As CRAXDRT.Report
Set oCRapp = New CRAXDRT.Application
Set oReport = oCRapp.OpenReport(ReportPath, crOpenReportByTempCopy)
SetReportDatabase oReport, DataPath
crView.ReportSource = oReport
crView.ViewReport
End Sub
Public Sub SetReportDatabase(CrystalRpt As CRAXDRT.Report, DataPath As String)
Dim oTab As CRAXDRT.DatabaseTable
On Error GoTo errhndl
For Each oTab In CrystalRpt.Database.Tables
' check connection type
If LCase$(oTab.DllName) = "crdb_odbc.dll" Then
With oTab.ConnectionProperties
.DeleteAll
.Add "Connection String", "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" & DataPath & ";Uid=Admin;Pwd=MyPassword"
End With
End If
Next oTab
' subreports
Dim rptObj As Object, rptObjs As CRAXDRT.ReportObjects, rptSecs As CRAXDRT.Sections, rptSec As CRAXDRT.Section
Dim subRptObj As CRAXDRT.SubreportObject, oSubTab As CRAXDRT.DatabaseTable
Dim subRpt As CRAXDRT.Report
Set rptSecs = CrystalRpt.Sections
For Each rptSec In rptSecs
Set rptObjs = rptSec.ReportObjects
For Each rptObj In rptObjs
If rptObj.Kind = crSubreportObject Then
Set subRptObj = rptObj
Set subRpt = subRptObj.OpenSubreport
For Each oSubTab In subRpt.Database.Tables
If oSubTab.DllName = "crdb_odbc.dll" Then
With oSubTab.ConnectionProperties
.DeleteAll
.Add "Connection String", "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" & DataPath & ";Uid=Admin;Pwd=MyPassword"
End With
End If
Next oSubTab
End If
Next rptObj
Next rptSec
Exit Sub
errhndl:
Err.Raise Err.Number, "SetReportDatabase", Err.Description
End Sub
Dim NumSubReports As Integer
Dim i As Integer
NumSubReports = crs.GetNSubreports
If NumSubReports > 0 Then
For i = 0 To NumSubReports - 1
'MsgBox crs.GetNthSubreportName(i)
crs.SubreportToChange = crs.GetNthSubreportName(i)
crs.Connect = mConnectionString
Next i
crs.SubreportToChange = ""
End If