Solved

VB6 and Crystal Reports 8

Posted on 2000-03-30
8
780 Views
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.
0
Comment
Question by:tdove
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Expert Comment

by:kthacker
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
0
 
LVL 4

Expert Comment

by:gcs001
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 & "}"

0
 

Author Comment

by:tdove
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 & "#"
0
 

Expert Comment

by:kthacker
ID: 2673469
Instead of using Parameters in my example above you use the .SQLQuery property and pass it your query.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Accepted Solution

by:
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"
                txtPSFromDate.SetFocus
                GoTo RunCastListExit
            Else
                txtPSFromDate.Text = txtPSToDate.Text
            End If
        Else
            If Not IsDate(txtPSToDate.Text) Then
                txtPSToDate.Text = txtPSFromDate.Text
            End If
        End If
    Else
        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) = "%"
    Else
        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
' else, we're going directly to the printer
        Report.Destination = crptToPrinter
        Unload newReport
    End If
    Report.Action = 1
               
RunCastListExit:
    Exit Sub

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

End Sub
0
 

Author Comment

by:tdove
ID: 2675979
Adjusted points from 200 to 250
0
 

Author Comment

by:tdove
ID: 2675980
mdougan-

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?
0
 
LVL 18

Expert Comment

by:mdougan
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now