WdnUlik2no
Programmer
I have a VB6 application that is able to view a Crystal report using the database that is defined in the .rpt file. This works fine and is able to generate updated reports as the DB changes, but we have many different databases (test, prod, dev, etc), and I have been trying since last night to get the VB app to change the reports database at runtime so I don't have to go in the report and manauly change that database location everytime or create a seperate report for each enviroment. I tried the solution on this site (faq-768-5374), but the app is still using the database defined in the .rpt file.
Here is the code:
Dim crxApp As New CRAXDRT.Application
Dim crxRpt As CRAXDRT.Report
Dim crxTables As CRAXDRT.DatabaseTables
Dim crxTable As CRAXDRT.DatabaseTable
Dim crxSubreportObject As CRAXDRT.SubreportObject
Dim crxSubReport As CRAXDRT.Report
Dim crxSections As CRAXDRT.Sections
Dim crxSection As CRAXDRT.Section
Private Sub Command1_Click()
Viewer.Refresh
End Sub
Private Sub Form_Load()
'Variable declarations
Dim strServerOrDSNName As String
Dim strDBNameOrPath As String
Dim strUserID As String
Dim strPassword As String
strServerOrDSNName = "dev.ourapp.com/appdev"
strDBNameOrPath = "com.mysql.jdbc.Driver;"
strUserID = "appuser"
strPassword = "apppassword"
Set crxRpt = crxApp.OpenReport("C:\test.rpt")
crxRpt.Database.Tables(1).SetLogOnInfo strServerOrDSNName, _
strDBNameOrPath, strUserID, strPassword
Set crxTables = crxRpt.Database.Tables
For Each crxTable In crxTables
With crxTable
.Location = .Name
End With
Next
'Loop through the Report's Sections to find any subreports, _
and change them as well
Set crxSections = crxRpt.Sections
For i = 1 To crxSections.Count
Set crxSection = crxSections(i)
For j = 1 To crxSection.ReportObjects.Count
If crxSection.ReportObjects(j).Kind = crSubreportObject Then
Set crxSubreportObject = crxSection.ReportObjects(j)
'Open the subreport, and treat like any other report
Set crxSubReport = crxSubreportObject.OpenSubreport
'*****************************************
Set crxTables = crxSubReport.Database.Tables
For Each crxTable In crxTables
With crxTable
.SetLogOnInfo strServerOrDSNName, _
strDBNameOrPath, strUserID, strPassword
.Location = .Name
End With
Next
'****************************************
End If
Next j
Next i
'View the report
Viewer.ReportSource = crxRpt
Viewer.ViewReport
End Sub
No matter what I set strServerOrDSNName and strDBNameOrPath to, the report still connects to our test DB as defined in the .rpt file. In fact if I leave these two fields blank it still connects the the test DB with no problem. What am I doing wrong? Thanks.
Here is the code:
Dim crxApp As New CRAXDRT.Application
Dim crxRpt As CRAXDRT.Report
Dim crxTables As CRAXDRT.DatabaseTables
Dim crxTable As CRAXDRT.DatabaseTable
Dim crxSubreportObject As CRAXDRT.SubreportObject
Dim crxSubReport As CRAXDRT.Report
Dim crxSections As CRAXDRT.Sections
Dim crxSection As CRAXDRT.Section
Private Sub Command1_Click()
Viewer.Refresh
End Sub
Private Sub Form_Load()
'Variable declarations
Dim strServerOrDSNName As String
Dim strDBNameOrPath As String
Dim strUserID As String
Dim strPassword As String
strServerOrDSNName = "dev.ourapp.com/appdev"
strDBNameOrPath = "com.mysql.jdbc.Driver;"
strUserID = "appuser"
strPassword = "apppassword"
Set crxRpt = crxApp.OpenReport("C:\test.rpt")
crxRpt.Database.Tables(1).SetLogOnInfo strServerOrDSNName, _
strDBNameOrPath, strUserID, strPassword
Set crxTables = crxRpt.Database.Tables
For Each crxTable In crxTables
With crxTable
.Location = .Name
End With
Next
'Loop through the Report's Sections to find any subreports, _
and change them as well
Set crxSections = crxRpt.Sections
For i = 1 To crxSections.Count
Set crxSection = crxSections(i)
For j = 1 To crxSection.ReportObjects.Count
If crxSection.ReportObjects(j).Kind = crSubreportObject Then
Set crxSubreportObject = crxSection.ReportObjects(j)
'Open the subreport, and treat like any other report
Set crxSubReport = crxSubreportObject.OpenSubreport
'*****************************************
Set crxTables = crxSubReport.Database.Tables
For Each crxTable In crxTables
With crxTable
.SetLogOnInfo strServerOrDSNName, _
strDBNameOrPath, strUserID, strPassword
.Location = .Name
End With
Next
'****************************************
End If
Next j
Next i
'View the report
Viewer.ReportSource = crxRpt
Viewer.ViewReport
End Sub
No matter what I set strServerOrDSNName and strDBNameOrPath to, the report still connects to our test DB as defined in the .rpt file. In fact if I leave these two fields blank it still connects the the test DB with no problem. What am I doing wrong? Thanks.