Crystal reports 8 – set location dynamically in vb6

crystal-reportsvb6

I have a VB6 front end, SQL Server 2005 as the back end and Crystal Reports 8.5 for reports.

I need to set the location at run time in my application as I have 2 databases. My problem is that when I change database, but the location remain the same. It will be great if anyone can help me out. Thanks in advance for your time, and here is my code.

Private Sub prin_Click()
With CrystalReport1
    .Connect = MDI1.txtcn --> this is my connection info "driver={sql server};server=server;database=database;uid=user;pwd=password"        
    .DiscardSavedData = True
    .Action = 1
    .PrintReport
End With

Best Answer

Try some code like this:

Private Sub cmdSetLocations_Click()
    Dim CrxApp As New CRAXDRT.Application
    Dim CrxRep As CRAXDRT.Report
    Dim CrxSubRep As CRAXDRT.Report

    Dim strReport As String
    Dim i As Integer, ii As Integer

    strReport = "[Path to report file]"
    Set CrxRep = CrxApp.OpenReport(strReport)

    SetReportLocation CrxRep

    For i = 1 To CrxRep.Sections.Count
        For ii = 1 To CrxRep.Sections(i).ReportObjects.Count
            If CrxRep.Sections(i).ReportObjects(ii).Kind = crSubreportObject Then
                Set CrxSubRep = CrxRep.OpenSubreport(CrxRep.Sections(i).ReportObjects(ii).SubreportName)
                SetReportLocation CrxSubRep
            End If
        Next ii
    Next

    'open your report in the report viewer

    Set CrxApp = Nothing
    Set CrxRep = Nothing
    Set CrxSubRep = Nothing
End Sub

Private Sub SetReportLocation(ByRef RepObj As CRAXDRT.Report)
    Dim CrxDDF As CRAXDRT.DatabaseTable
    Dim CP As CRAXDRT.ConnectionProperties

    For Each CrxDDF In RepObj.Database.Tables
        Set CP = CrxDDF.ConnectionProperties
        CP.DeleteAll
        CP.Add "Connection String", "[Your connection string goes here]"
    Next

    Set CrxDDF = Nothing
    Set CP = Nothing

End Sub
Related Topic