Search code examples
ms-accessvb6crystal-reports-8.5

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


I wanted to open the CR, while clicking a button on the form in VB 6.0. This is the code i've used:

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

But when I try to execute i ran into "Cannot open SQL server" error. But I've used Access as the database file. I wanted only to open the CR Showing the contents of a particular table. I'm using CR 8.5. Can anyone help me regarding this?


Solution

  • This may Help you

    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