VB.NET Crystal Reports Date parameter

Before commencing this tutorial, it is essential to familiarize yourself with the database structure. All the Crystal Reports programming samples presented in this tutorial are based on the "crystaldb" database. To gain a better understanding of its structure, please refer to the following link: Database Structure .

In this tutorial, we will explore the process of passing a date variable to Crystal Reports from VB.NET. It is important to note that this tutorial builds upon the previous tutorials that covered passing a string parameter and an integer parameter to Crystal Reports. To fully grasp the concepts and techniques involved, I recommend reviewing those tutorials before proceeding.

When passing a date parameter, the first step is to create a new date parameter within the Parameter Fields section of the Field Explorer. By doing so, you will be presented with the following screen, where you need to fill in the required fields as illustrated in the provided picture. This step allows you to define the necessary parameter configuration within Crystal Reports.

vb.net_crystal_report_date_parameter_1.GIF

Once the parameter field for the date has been created, the next step is to create the selection formula for Crystal Reports. To accomplish this, right-click on the Crystal Reports designer window and navigate to REPORT -> SELECTION FORMULA -> RECORD.

Upon selecting these options, the Record Selection Formula Editor will appear, providing you with a space to input the desired selection formula. To construct the formula, you will need to select the relevant fields from the available lists and combine them appropriately.

In this tutorial, we will create a formula that selects all record details from the tables where the order date is greater than the input date parameter. To achieve this, begin by selecting the field "Ordermaster.orderdate" from the available fields in the Formula Editor. Next, choose the appropriate comparison operator (e.g., ">" for greater than) and select the parameter date field that was created earlier. By selecting these fields in the Formula Editor, they will be added to the formula.

By following these steps, you will successfully construct the selection formula that filters the records based on the specified condition, where the order date is greater than the input date parameter. This allows you to retrieve and display only the relevant data in the Crystal Reports based on the provided date parameter.

vb.net_crystal_report_date_parameter_2.GIF

After the creation of selection formula close that screen .

Now the designing part is over and the next step is to call the created Crystal Reports in VB.NET through Crystal Report Viewer control .

Select the default form (Form1.vb) you created in VB.NET and drag a Textbox , button and CrystalReportViewer control to your form.

Select Form's source code view and import the following :

  1. Imports CrystalDecisions.CrystalReports.Engine
  2. Imports CrystalDecisions.Shared

Put the following source code in the button click event

Full Source VB.NET
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim cryRpt As New ReportDocument cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") Dim crParameterFieldDefinitions As ParameterFieldDefinitions Dim crParameterFieldDefinition As ParameterFieldDefinition Dim crParameterValues As New ParameterValues Dim crParameterDiscreteValue As New ParameterDiscreteValue crParameterDiscreteValue.Value = TextBox1.Text crParameterFieldDefinitions = _ cryRpt.DataDefinition.ParameterFields crParameterFieldDefinition = _ crParameterFieldDefinitions.Item("Orderdate") crParameterValues = crParameterFieldDefinition.CurrentValues crParameterValues.Clear() crParameterValues.Add(crParameterDiscreteValue) crParameterFieldDefinition.ApplyCurrentValues(crParameterValues) CrystalReportViewer1.ReportSource = cryRpt CrystalReportViewer1.Refresh() End Sub End Class
NOTES:

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here.

Now you can run the program . Enter any date , then you can see the report whose order date is greater than or equal to the entered date.

vb.net_crystal_report_date_parameter_3.GIF

Here we got the result of orders whose date is greater than the entered date