Sunteți pe pagina 1din 3

I have been using this forum for a while but only joined yesterday to post this information.

Like most people I was having great difficulty in getting SQL Serve r 2000, Crystal Reports 8.5 and Visual Basic 6 to work together to produce repor ts for the end user. My main problem was the error 'SQL Server has not yet been opened'. This was due to my report having subreports within it, which nobody any where has properly explained. As this took a good bit of searching and also my own coding to resolve this I th ought I would post the end result on here for anyone else who was having the sam e difficulties. 2 Ways to do this are... Passing a SQL Query to a report or by Passing parameter s from a users input. First of all make sure that the Component Crystal Reports Viewer Control has bee n added to your project. Next make sure the 2 References - Crystal Reports Viewe r Control and Crystal Reports 8.5 ActiveX Designer Run-Time Library have been ad ded. Now for the code... Add the following to the general declarations area: Code: '*************************************************************************** ************* ' General Declarations for working with Crystal Reports '*************************************************************************** ************* 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 Dim crParamDefs As CRAXDRT.ParameterFieldDefinitions Dim crParamDef As CRAXDRT.ParameterFieldDefinition Next place the following code behind the command button, REMOVING the code Way 1 or Way 2 depending on your application. Code: Private Sub cmdSubmit_Click() '*************************************************************************** ************* ' Variables declarations for the Database Connection '*************************************************************************** ************* Dim strServerOrDSNName As String Dim strDBNameOrPath As String Dim strUserID As String

Dim strPassword As String strServerOrDSNName = "servername" nection server strDBNameOrPath = "databasename" nnection databasename strUserID = "userid" on userid strPassword = "password" tion password 'replace servername with your SQL con 'replace databasename with your SQL co 'replace userid with your SQL connecti 'replace password with your SQL connec

'Open the report Set crxApp = New CRAXDRT.Application Set crxRpt = crxApp.OpenReport("C:\test.rpt") 'Set the connection for the report. 'SetLogOnInfo is a deprecated method in CR 9, but still works. 'The suggested method for CR9 is to use the ConnectionProperty object. crxRpt.Database.Tables(1).SetLogOnInfo strServerOrDSNName, strDBNameOrPath, st rUserID, strPassword 'This removes the schema from the Database Table's Location property. Set crxTables = crxRpt.Database.Tables For Each crxTable In crxTables With crxTable .Location = .Name End With Next Way 1 - Passing a SQL Query string '***************************************Start of Way 1********************** *************** 'The select query to reutrn the data required crxRpt.SQLQueryString = "SELECT whatever FROM table WHERE blah blah" 'Input yo ur SELECT query here '****************************************End of Way 1*********************** ************** Way 2 - Passing Parameters from a users input '***************************************Start of Way 2********************** *************** ' Disable Parameter Prompting for the end user crxRpt.EnableParameterPrompting = False ' Gather the list of available parameters from the report Set crParamDefs = crxRpt.ParameterFields ' Loop through all parameters in the report by name, filling in the appropri ate parameter with the right value For Each crParamDef In crParamDefs Select Case crParamDef.ParameterFieldName Case "JobNumber" crParamDef.SetCurrentValue txtJobNumber.Text Case "POStatus" crParamDef.SetCurrentValue POStatus

Case "PODetail" crParamDef.SetCurrentValue PODetail Case "WOStart" crParamDef.SetCurrentValue txtWOStart.Text Case "WOEnd" crParamDef.SetCurrentValue txtWOEnd.Text End Select 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, strUs erID, strPassword .Location = .Name End With Next End If Next j Next i '****************************************End of Way 2*********************** ************** 'View the report CRViewer1.ReportSource = crxRpt CRViewer1.ViewReport 'Set the Zoom on the report CRViewer1.Zoom (90) End Sub Way 2 involves passing parameters to your report, this means your reports MUST u se these parameters within the report.

S-ar putea să vă placă și