Search code examples
vb6ms-access-2013crystal-reports-8.5

How to make a connection string for CR 8.5 using VB6.0


I wanted to know how to make a Connection String statement for Crystal Reports 8.5 using Visual Basic 6. I've tried this code:

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

But I couldn't understand the 4th line. Can someone explain me about this. I am using MS Access 2013 as Database. Any help would be much appreciated.


Solution

  • With some modifications this should work:

    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