VB6 and Crystal Reports 8

Posted on 2000-03-30
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 & "#"
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 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