"Cannot open SQL server" 错误 -- 但使用 Access?

"Cannot open SQL server" error -- but using Access?

我想打开 CR,同时单击 VB 6.0 中表单上的按钮。这是我使用的代码:

 CrystalReport1.ReportFileName = "D:\VISUAL BASIC\monrep.rpt"
 CrystalReport1.RetrieveDataFiles
 CrystalReport1.Action = 1

但是当我尝试执行 i 运行 时出现“无法打开 SQL 服务器”错误。但我使用 Access 作为数据库文件。我只想打开显示特定 table 内容的 CR。我正在使用 CR 8.5。谁能帮我解决这个问题?

这可能对你有帮助

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