Solved

How to pass VB parameter to SQLQuery in Crystal Report.

Posted on 2001-06-30
13
2,509 Views
Last Modified: 2013-12-25
I am using VB6.

I am trying to give the user the ability to choose the records that Crystal will print.
E.G SELECT * FROM Mydatabase where TransDate > 12/12/98

I will like to have the user pass their selected values (date)to a textbox or a Maskedbox and have VB pass the values to Crystal Report. Crysyal should use those values as its criteria for record selection and generate the report.

Thank you.


Please kindly help to give some example to associate VB variable in the Crystal report SQLquery
0
Comment
Question by:belim
  • 7
  • 2
  • 2
  • +2
13 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6242352
0
 

Author Comment

by:belim
ID: 6243429
Emoreau,
  I unable to access those page, any problem with the address?
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6244603
I think you could do this by setting the recordselectionformula from VB but I do it by declariung a formula field in the report to hold the value - setting that in VB and utilising it in the report.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6244964
I just accessed them without problems! Here is a paste of both.

Knowledge Base
 Article ID:
c2004910

Created:
1999/11/02

Reviewed :
2000/06/16
 

 
 
 
 
 
Using parameters to change the logic of the record selection criteria

The information in the article refers to:
Seagate Crystal Reports 7
 
 
Applies to:
 
Reported version and lower
Version 5, 6 and 7
Seagate Info Report Designer
Using parameters to create dynamic selection formulas
 

Synopsis


A report needs the ability to change the logic of its record selection formula based on a user-specified parameter value. For example, you can choose whether the record selection criteria is for a database field to be greater than a value, or for it be less than a value, depending on what you enter for the parameter:

  Select {field} > Value or
  Select {field} < Value

Solution

You can do this by creating a parameter field and then creating an 'if then else' statement in the record selection formula.

NOTE:=======

Using a 'If then Else' formula for your record selection prevents the record selection formula from being passed on to the SQL statement's WHERE clause. This means the record selection process will occur in SCR, instead of in the database. This could significantly increase the time it takes your report to process.

=============

1. Create a string parameter {?condition}, setting the default values to:

<
>
<=
>=
<>

2. Go to Report | Edit Selection Formula, and select Record. Create the following formula:

Note: this example uses the value 10,000 for the record comparisons, although this value could also be parameterized as well, for greater flexibility.

If {?condition} = "<" then
         {database field} < 10000 else
If {?condition} = ">" then
         {database field} > 10000 else
If {?condition} = "<=" then
          {database field} <= 10000 else
If {?condition} = ">=" then
          {database field} >= 10000 else
If {?condition} = "<>" then
{database field} <> 10000;

This selects records using a criteria based on the value entered for the parameter.



--------------------------------------------------------------------------------

Category:
 Report Designer , Report Designer , Designers , Designers
 
Subject:
 Filtering Data , Parameter Fields , Info Report Designer , Info Report Designer
 
Topic:
 Record Selection , Crystal Parameter , Filtering Data , Parameter Fields
 
Keywords:
 RECORD SELECTION PARAMETER CHANGE LOGIC SELECTION CRITERIA
 
 
 


--------------------------------------------------------------------------------

Status:
 Verified
 
Product:
 Crystal Reports Professional
 
Reported Version:
 7.0.1.192 Crystal Reports Professional
 
Applies to:
 Reported version and lower
 
Bit Version:
 16 & 32 Bit
 
Applies To (Database):
 All
 
 
========================================================

Knowledge Base
 Article ID:
c2002292

Created:
1998/12/04

Reviewed :
2001/02/13
 

 
 
 
 
 
Passing Selection Formulas & Formulas at Runtime using Automation Servers

The information in the article refers to:
Report Designer Component 7
 
 
Applies to:
 
Reported version and higher
Formula Fields
Selection Formula

 

Synopsis

Passing Selection Formulas and Formulas at Runtime using the Crystal Report Engine Automation Server (Cpeaut32.dll) and the Crystal Report Designer Component Automation Server (Craxdrt.dll).

Solution

NOTE: Formulas must conform to Crystal Reports' formula syntax.(i.e. strings embedded in single quotes while numbers not, etc). For Crystal Reports to parse a formula it receives from the application (Visual Basic in this case), the formula must be identical to a formula that you would enter directly into the report designer.

  Use the MsgBox function in Visual Basic to verify that the formula string being sent from VB is in a format that can be used by Crystal Reports.

The remainder of this document shows some sample code, demonstrating the use of the SelectionFormula and Formulas() Properties of the CPEAUT32.DLL and CRAXDRT.DLL.

Passing Selection Formula to the Crystal Report using CPEAUT32.DLL
================================================

Selection Formula
----------------------------
Essentially when passing the selection formula to Crystal Reports using the CPEAUT32.DLL, the .RecordSelectionFormula property of the Report object is utilized.

When hardcoding the selection formula, the code can be directly copied from the Crystal Report and pasted into the code as follows:

Sub Command1_Click()
    CrpReport.RecordSelectionFormula = "{Customer.Customer ID} = 1"
    CrpReport.Preview
End Sub

If, however, you would like to replace the value with a variable, the code would look like the following:

Sub Command1_Click()
    'x can be a textbox being populated with an integer
    Dim x as integer
    CrpReport.RecordSelectionFormula = "{Customer.Customer ID}=" & x
    CrpReport.Preview
End Sub

This particular example demonstrates passing a integer variable to Crystal Reports, if however, the variable is type string, it is slightly different:

Sub Command1_Click()
    'x can be a textbox being populated with an integer
    Dim x as string
    CrpReport.RecordSelectionFormula = "{Customer.Customer ID}=" & chr(34) & x & chr(34)
    CrpReport.Preview
End Sub

Any dates that are being passed to Crystal Reports must be in Crystal Reports Date format, Date(yyyy,mm,dd).

FormulaFields
----------------------
When passing a value to Crystal Reports, the .Text property of the FormulaFieldDefinition object is used.

First, in Crystal Reports, create your formula. If you are giving it a string value, you must place a string value in it. This will force Crystal to treat the formula as a string. A single space inside double quotes is a good idea, as this will not print if left unchanged--yet it holds the string type for when you change the actual value later from VB. Place the new formula on your report where you would like the passed value displayed.

If you will be sending a numeric value or date, again place an appropriate value in the formula in order to establish the data type of the formula. To pass a numeric value to a formula, place 0 in the formula when you create the formula in Crystal Reports. To pass a date value to a formula, place the Today function in the formula.


Consider that you would like to send a title to your report from your VB application. The following is the suggested syntax to change the title at runtime. As instructed above, a formula is inserted at the top of the report, and is named Title. It presently contains a space inside quotes, as in " ".

Sub Command1_Click ()
    'if we hard-code the title
    CrReport.FormulaFields.Item(1).text = " 'This is a Title' "
    'If we use the value of the Textbox
    Text1.Text = "This is a Title"
    CrReport.FormulaFields.Item(1).text = chr(34) & text1.text & chr(34)
    CrReport.Preview
End Sub

Now you would like to send a numeric value to your report. The following code changes the value of a numeric formula at runtime.

Sub Command1_Click ()
    'if we hard-code the numeric value
     CrReport.FormulaFields.Item(1).text = "9"
    'If we use the value of a variable
     x = 65
     CrReport.FormulaFields.Item(1).text = x
     CrReport.Preview
End Sub

Passing Selection Formula and Formulas using CRAXDRT.DLL
===========================================

Passing the selection formula to the Crystal Report using the CRAXDRT.DLL is almost the same. Except for fully qualifying the report objects differently, the syntax is essentially the same. If for example, you are passing the selection formula from the form_load event, the code would look like the following:

'General Declarations
Dim crxReport as New CrystalReport1

Private Sub Form_Load()
CRViewer1.ReportSource = report
report.RecordSelectionFormula = "{Customer.Customer ID} = 1"
CRViewer1.ViewReport
End Sub

The same rules apply to the CRAXDRT.DLL in regards to passing strings, numbers and dates (see above.)

Like the RecordSelectionFormula, passing the values to formula fields are basically the same as the CPEAUT32.DLL except for fully qualifying the variables differently. See below:

'General Declarations
Dim crxReportas New CrystalReport1
Dim CrFormulaFields as CRAXDRT.FormulaFields
Dim CrFormulaField as CRAXDRT.FormulaField

Private Sub Form_Load()
CRViewer1.ReportSource = report
Set CrFormulaFields = report.FormulaFields
Set CrFormulaField = CrFormulaFields.Item(1)
CrFormulaField.Text = " 'This is a String' "
CRViewer1.ViewReport
End Sub

Again, the same rules in regards to datatypes apply for passing values to formula fields as it does for CPEAUT32.DLL.



--------------------------------------------------------------------------------

Category:
 Development , Development
 
Subject:
 Report Designer Component (RDC) , Automation
 
Topic:
 Formulas , Formulas
 
Keywords:
 SCRCOM, RECORDSELECTIONFORMULA, FORMULA, AUTOMATION SERVER, RDC
 
 
 


--------------------------------------------------------------------------------

Status:
 Reported Issue
 
Product:
 Crystal Reports Professional
 
Reported Version:
 7.0.1.192 Crystal Reports Professional
 
Applies to:
 Reported version and higher
 
Bit Version:
 32 Bit
 
Reported OS and Version:
 Windows 95
 
Dev Tool:
 Visual Basic
 
 
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6257547
The easiest way would be to change the SelectionFormula property of the Report control before you preview the Report through VB.  There are many other ways.  One way would be to base your report on a Stored procedure, and then set the StoredProcParams before you preview the report (this is my favorite way to do it).  Or, you can create a couple of Formula fields in your report, and in the report you build a selection formula that selects records in the range of date 1 to date 2.  This is a little more work.

Back to the easy way.  Here is an example of exactly what you asked for:

Assume that you have a text box where they enter the beginning date of the date range (From Date) and another for the ending date of the date range (To Date) and let's say that your table name was called Orders and your date field is called OrderDate.  Then, your SelectionFormula would look like this.

Dim vFromDate as variant
Dim vToDate as variant

vFromDate = txtFromDate.Text
vToDate = txtToDate.Text

Report1.SelectionFormula = "{Orders.OrderDate} in " & _
"Date(" & Year(vFromDate) & ", " & Month(vFromDate) & ", " & _
Day(vFromDate) & ") to " & _
"Date(" & Year(vToDate) & ", " & Month(vToDate) & ", " & _
Day(vToDate) & ")"

Report1.Action = 1

If you put a breakpoint on that line, then when you display the contents of Report1.SelectionFormula (after executing that statement) the contents should look like:

{Orders.OrderDate} in Date(2001,7,4) to Date(2001,7,11)
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6257767
Is belim still around?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 18

Expert Comment

by:mdougan
ID: 6257924
Good question.  By the way, Belim, don't try manipulating the SQLQuery, it never seems to work.  Stick to the SelectionFormula, or to StoredProcParams....
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6263093
Any news now?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6290131
Questions Asked 6
Last 10 Grades Given A B B  
Question Grading Record 3 Answers Graded / 3 Answers Received
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 6305154
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6331654
It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
 
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
 
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
 
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
 
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
 
PLEASE DO NOT AWARD THE POINTS TO ME.
 
____________________________________________
 
 
 
Hi Experts:
 
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
 
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
 
Thank you everyone.
 
Moondancer :)
Community Support Moderator @ Experts Exchange
 
P.S.  Engineering has been advised about the error in the comment date/time sort order.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6331809
I deserve the points
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6338641
Force accepted by moderator.  In the event Asker returns with additional needs related to this question, please respond and continue the collaboration process.
Thank you,
Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now