What I need is to create the SQL and DB connection within VB based on Users input
and pass this SQL into an .RPT file and populate the report.
The .RPT file was created using Crystal Designer Version 8.5 outside of VB.
The .Rpt file have all the liked tables defined and there is an SQL statement related to it.
When you go to Database / Show SQL Query you will see that SQL generated by Crystal. As this is
how you will get a Report defined, you have to go through all these.
My problem is when I ran my report, the report will always show that same report
that was ran in Crystal during design time, I used the following code that I have gathered from
books and at Crystals Web site.(See Below)
I thought that with RDC, we could just pass in the ADO Record set and the report should reflect
by what data was brought back from the record set open statement.
Well, the code below is what I used. The SQL statement in the Variable MAINSQL is tested each
time by me within SQL Analyzer and is found to be be correct. And after the ADO recordset is Opened,
I physically dumped the ADO recordset and the data is identical to the result from the SQL Analyzer.
The Open Connection is checked and each time it is opened.
So what am I doing wrong ?? Could it be that :
CrxReport.Database.SetDataSource Adors, 3, 1
CrxReport.ReadRecords
is not the correct way ???
I appreciate any help or direction that you could give.
Thank You
------ Below is Code Used --------
Dim CrxApplication As CRAXDRT.Application
Dim CrxReport As CRAXDRT.Report
MainSQL = < My SQL Statement are linked to 6 tables > Check against SQL Analyzer as Correct
' With Correct Data being returned
Set CrxApplication = CreateObject("CrystalRuntime.Application"
Set CrxReport = CrxApplication.OpenReport(App.Path & "\" & "First.RPT"
Dim RptCNN As Connection
Dim DatCMD1 As ADODB.Command
Set RptCNN = New Connection
Dim DB_Name As String
Dim FilePath As String
Dim Server_Name As String
Dim ODBCDriver As String
Dim LocalMachine As String
Dim IndividualRS As ADODB.Recordset
DB_Name = "BenServer"
Server_Name = "Mach4672"
ODBCDriver = "SQL SERVER"
Provider = "SQLOLEDB"
With RptCNN
ODBCDriver = "SQL Server"
.Provider = "SQLOLEDB"
uid = "SA"
pwd = ""
.ConnectionString = "Driver={" & ODBCDriver & "};Server=" & Server_Name & ";Database=" & DB_Name _
& ";UID=" & uid & ";PWD=" & pwd
.Mode = adModeRead
.CursorLocation = adUseClient
.Open
End With
If RptCNN.State <> 1 Then
MsgBox " Connection Open Error ", vbCritical
Exit Sub
End If
CrxReport.DiscardSavedData
Dim Adors As New ADODB.Recordset
Set Adors = New ADODB.Recordset
With Adors
'Use the connection already opened
.ActiveConnection = RptCNN
.CursorType = adOpenDynamic
.Open MainSQL
End With
'' At this point I checked the Recordset, Its got the correct data as specified
in the SQL statement MainSql ....
If Adors.State = 1 Then
CrxReport.Database.SetDataSource Adors, 3, 1
CrxReport.ReadRecords
Else
'Debug.Print Err.Description
MsgBox " Record Set Open Failed ", vbCritical
Exit Sub
End If
Screen.MousePointer = vbHourglass
CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport
End Sub
and pass this SQL into an .RPT file and populate the report.
The .RPT file was created using Crystal Designer Version 8.5 outside of VB.
The .Rpt file have all the liked tables defined and there is an SQL statement related to it.
When you go to Database / Show SQL Query you will see that SQL generated by Crystal. As this is
how you will get a Report defined, you have to go through all these.
My problem is when I ran my report, the report will always show that same report
that was ran in Crystal during design time, I used the following code that I have gathered from
books and at Crystals Web site.(See Below)
I thought that with RDC, we could just pass in the ADO Record set and the report should reflect
by what data was brought back from the record set open statement.
Well, the code below is what I used. The SQL statement in the Variable MAINSQL is tested each
time by me within SQL Analyzer and is found to be be correct. And after the ADO recordset is Opened,
I physically dumped the ADO recordset and the data is identical to the result from the SQL Analyzer.
The Open Connection is checked and each time it is opened.
So what am I doing wrong ?? Could it be that :
CrxReport.Database.SetDataSource Adors, 3, 1
CrxReport.ReadRecords
is not the correct way ???
I appreciate any help or direction that you could give.
Thank You
------ Below is Code Used --------
Dim CrxApplication As CRAXDRT.Application
Dim CrxReport As CRAXDRT.Report
MainSQL = < My SQL Statement are linked to 6 tables > Check against SQL Analyzer as Correct
' With Correct Data being returned
Set CrxApplication = CreateObject("CrystalRuntime.Application"
Set CrxReport = CrxApplication.OpenReport(App.Path & "\" & "First.RPT"
Dim RptCNN As Connection
Dim DatCMD1 As ADODB.Command
Set RptCNN = New Connection
Dim DB_Name As String
Dim FilePath As String
Dim Server_Name As String
Dim ODBCDriver As String
Dim LocalMachine As String
Dim IndividualRS As ADODB.Recordset
DB_Name = "BenServer"
Server_Name = "Mach4672"
ODBCDriver = "SQL SERVER"
Provider = "SQLOLEDB"
With RptCNN
ODBCDriver = "SQL Server"
.Provider = "SQLOLEDB"
uid = "SA"
pwd = ""
.ConnectionString = "Driver={" & ODBCDriver & "};Server=" & Server_Name & ";Database=" & DB_Name _
& ";UID=" & uid & ";PWD=" & pwd
.Mode = adModeRead
.CursorLocation = adUseClient
.Open
End With
If RptCNN.State <> 1 Then
MsgBox " Connection Open Error ", vbCritical
Exit Sub
End If
CrxReport.DiscardSavedData
Dim Adors As New ADODB.Recordset
Set Adors = New ADODB.Recordset
With Adors
'Use the connection already opened
.ActiveConnection = RptCNN
.CursorType = adOpenDynamic
.Open MainSQL
End With
'' At this point I checked the Recordset, Its got the correct data as specified
in the SQL statement MainSql ....
If Adors.State = 1 Then
CrxReport.Database.SetDataSource Adors, 3, 1
CrxReport.ReadRecords
Else
'Debug.Print Err.Description
MsgBox " Record Set Open Failed ", vbCritical
Exit Sub
End If
Screen.MousePointer = vbHourglass
CRViewer1.ReportSource = CrxReport
CRViewer1.ViewReport
End Sub