VB6 and Crystal Reports 8

Posted on 2000-03-30
Medium Priority
Last Modified: 2013-12-25
Thinking it would be easier I had my company buy Crystal Reports 8 to do my reporting.  I am still having some problems.  Can someone give me an example code how to pass variables from a form that has controls(datacomboBox and DTPicker) to a Crystal Report. Thanks In Advance.
Question by:tdove
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 2670818
I usually put the CrystalReport .ocx control on a separate form, then when I need to print a report I load the form (but do not show it), and set the properties as demonstrated below.

This is to run a report on a SQL Server database.

The ParameterFields property must match up the parameter fields in the report.  This is where you can pass variables from the controls on your form to the report.  If you have more than one then you just add more .ParameterFields().

If you want to preview the report on screen just change the Destination property.

With frmCrystalReports.cryReport
   .ReportFileName = "ReportName"
   .Destination = crptToPrinter
   .Connect = "DSN = " & "ServerName" & ";UID = " & "UserName" & ";PWD = " & "UserPassword" & ";DSQ = " & "DatabaseName"
   .ParameterFields(0) = "ParameterName1;" & "ParameterValue1" & ";TRUE"
   .ParameterFields(1) = "ParameterName2;" & "ParameterValue2" & ";TRUE"
   .Action = 1
End With

Expert Comment

ID: 2671157
You could also pass these values to the Crystal Report using the SelectionFormaula method:

CrystalReport1.SelectionFormula = "{Table1.Start_Date = " & DatePicker1.Value & "} AND {Table1.Other_Field = " & DataCombo1.Text & "}"


Author Comment

ID: 2672635
What I want to do is send the following SQL query to the crystal report, so it would only show the records specified in the query.  Would I still do it one of the ways shown above?

"SELECT * FROM Employees WHERE RegionCat ='" & DataCombo1.Text & "' and DteExp BETWEEN #" & DTPicker2.Value & "# and #" & DTPicker1.Value & "#"
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2673469
Instead of using Parameters in my example above you use the .SQLQuery property and pass it your query.
LVL 18

Accepted Solution

mdougan earned 750 total points
ID: 2674526
You didn't say what database you're using but if you're using SQL Server, then I'd highly recommend creating your report based on a Stored Procedure, or, if you're using ACCESS, I'd recommend creating your report based on a QUERY that takes PARAMETERS (both of these will work the same from Crystal).

In doing this, you don't have to mess with the Crystal SQL Query at all, and you can make changes to your stored proc without hosing your report.  If you do this, then you'll pass your criteria to the stored procedure or QUERY in the Crystal controls StoredProcParam() array.  Below is an example of an entire report function that takes the criteria from textboxes, or combos on the form, opens up a new report form, generates the report.

Public Sub RunCastList()
Dim sErrors As String
Dim lError As Variant

' This is just a blank form that will be used to display the report
' it allows us to dynamically resize the report window (I have an elastic control on it
Dim newReport As New frmReport

    On Error GoTo RunCastListErr
' Just validating some report criteria
    If txtPSScriptNumber.Text = "" Then
        If Not IsDate(txtPSFromDate.Text) Then
            If Not IsDate(txtPSToDate.Text) Then
                MsgBox "You must enter either a Script Number, a From Date, or a From and To range of dates to report on.", vbCritical, "Cast List Report"
                GoTo RunCastListExit
                txtPSFromDate.Text = txtPSToDate.Text
            End If
            If Not IsDate(txtPSToDate.Text) Then
                txtPSToDate.Text = txtPSFromDate.Text
            End If
        End If
        If txtPSFromDate.Text <> "" Or txtPSToDate.Text <> "" Then
            MsgBox "If you enter a script number, From and To dates will be ignored.", vbInformation, "Cast List Fees Report"
            txtPSFromDate.Text = ""
            txtPSToDate.Text = ""
        End If
    End If
' Making the Crystal report window a child of the main MDI window
    Report.DialogParentHandle = frmMain.hwnd
' Moving the report window to the Upper Left corner of the form
    Report.WindowLeft = 0
    Report.WindowTop = 0
' Use your regular connect string
    Report.Connect = gRptConnectString

    Report.ReportFileName = App.Path & "\REPORTS\NewCastList.RPT"

    Report.WindowTitle = "Cast List"
    Report.ReportTitle = "Cast List"
' This formula is just used as a display in the report page header
    Report.Formulas(0) = "ShowName = " & Chr$(34) & USER_SHOW & Chr$(34)
' Set a couple of the parameters for the stored procedure
    Report.StoredProcParam(0) = 0
    Report.StoredProcParam(1) = gUserShow

' note, Crystal is very specific about the format of datetime columns    
    Report.StoredProcParam(2) = Format(txtPSFromDate.Text, "yyyy-mm-dd") & " 00:00:00.000"
    Report.StoredProcParam(3) = Format(txtPSToDate.Text, "yyyy-mm-dd") & " 00:00:00.000"

' This formula just displays at the page header to show date ranges
    Report.Formulas(1) = "RPTCRITERIA = " & Chr$(34) & "From: " & Format(txtPSFromDate.Text, "mm/dd/yyyy") & "     To: " & Format(txtPSToDate.Text, "mm/dd/yyyy") & Chr$(34)
    End If

' User can select [ALL] or a particular one from a  combo
    If cboPSUnion.ListIndex < 1 Then
        Report.StoredProcParam(4) = "%"
        Report.StoredProcParam(4) = cboPSUnion.List(cboPSUnion.ListIndex)
    End If
' If we're displaying the Print Preview window
    If PREVIEWS = True Then
' set the reports window handle to the blank form we loaded
        Report.WindowParentHandle = newReport.vseReport.hwnd

' make the report window fill the blank form
        Report.WindowHeight = newReport.ScaleHeight
        Report.WindowWidth = newReport.ScaleWidth

        newReport.Caption = "Cast List"
        Report.Destination = crptToWindow
' else, we're going directly to the printer
        Report.Destination = crptToPrinter
        Unload newReport
    End If
    Report.Action = 1
    Exit Sub

    Unload newReport
    Screen.MousePointer = DEFAULT
    sErrors = "RunCastList " & Err.Description
    For Each lError In Errors
        sErrors = sErrors & vbCrLf & lError.Description
    MsgBox sErrors, vbCritical, Err & ""
    Resume RunCastListExit

End Sub

Author Comment

ID: 2675979
Adjusted points from 200 to 250

Author Comment

ID: 2675980

Thanks for your help!  You have shown me how to do it with OCX control.  Can you show me how you would do it with RDC?
LVL 18

Expert Comment

ID: 2819472
I might not be any help with that, as I don't know what the RDC is.  A Report Data Control?  Is it a Crystal component or is it the VB Data Report?  The only other thing I've worked a little bit with is the Crystal Print Engine API.  But, if you can show some code, and fill me in a little more on the background, then if it's Crystal related, I might be able to offer some suggestions.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question