crystal report 8 - 在vb6中动态设置位置

时间:2011-01-21 07:26:02

标签: vb6 crystal-reports

我有一个VB6前端,SQL Server 2005作为后端,Crystal Reports 8.5用于报告。

我需要在我的应用程序中设置运行时的位置,因为我有2个数据库。我的问题是,当我更改数据库时,但位置保持不变。如果有人能帮助我,那将是很棒的。提前感谢您的时间,这是我的代码。

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

4 个答案:

答案 0 :(得分:2)

尝试这样的代码:

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

答案 1 :(得分:1)

With CR
     .ReportFileName = App.Path + "\Labsen2.rpt"
     .SelectionFormula = "{PersonalCalendar.PersonalCalendarDate}>= Date(" & Year(DTPicker1) & "," & Month(DTPicker1) & "," & Day(DTPicker1) & ") and {PersonalCalendar.PersonalCalendarDate}<=date(" & Year(DTPicker2) & "," & Month(DTPicker2) & "," & Day(DTPicker2) & ") and {Department.DepartmentName}= '" & Combo1.Text & "'"
     .Formulas(0) = "tglAwal = '" & DTPicker1.Value & "'"
     .Formulas(1) = "tglAkhir = '" & DTPicker2.Value & "'"
     .Password = Chr(10) & "ithITtECH"
     .RetrieveDataFiles
     .WindowState = crptMaximized
     .Action = 1
 End With

答案 2 :(得分:0)

尝试格式化连接字符串,如下所示:

DSN=server;UID=database;PWD=password;DSQ=user

DSNUIDDSQ的含义是违反直觉的,它们会被Crystal超载。

同时检查您是否没有子Connect属性需要进行类似更改的子报表。

答案 3 :(得分:0)

为什么不将记录集传递给您的报告?通过这种方式,您将能够从任何受支持的数据库(我的意思是VB6可以连接到)数据库中获取数据,您甚至可以合并来自多个数据库的数据,您的报告将仅需要数据(记录集)并且将使用数据创建报告现场定义。