There are several ways to use parameters in ActiveReports 6.
To add parameters using the Report Explorer
To add parameters to a SQL query
You can allow users to filter the amount of information exposed in a report through the use of parameters. When you add SQL parameters to a report, ActiveReports displays an Enter Report Parameters dialog where the user can enter the values to pull from the database.
SQL Query. Paste in the Report Data Source window's Query box. |
Copy Code |
---|---|
SELECT * FROM Products INNER JOIN (Orders INNER JOIN [Order Details] ON Orders.OrderID= [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID WHERE Products.SupplierID = <%SupplierID|Enter Supplier ID|7%> AND OrderDate >= #<%OrderDate|Order date from|11/1/1994|D%># AND Discontinued = <%Discontinued|Is this checked?|true|B%> |
The SQL query above causes ActiveReports to display the following dialog to the user. The user can accept these or input other values to select report data.
To understand how this works, see SQL Query Parameters.
Note: Specifying the "param:" prefix for a parameter in the SQL query relates this parameter to the one in the Report Explorer. For example, "select * from customers where customername = '<%param:Parameter1%>'". In this case, the parameter with the "param:" prefix in the SQL query is updated with values of the corresponding parameter in the Report Explorer. |
SQL Query Parameters Syntax
The SQL query above contains three parameters:
Each of these parameters follow the syntactical pattern <%FieldName | PromptString | DefaultValue | Type | PromptUser%>
Note: Although FieldName is the only required parameter, if you do not specify a DefaultValue for each parameter, the Report Explorer is not populated with bound fields at design time. |
Tip: For Strings, if you specify a default value that is enclosed in apostrophes or quotation marks, ActiveReports sends the same marks to SQL. For Boolean parameters, if you specify true/false for the DefaultValue it generates true/false for SQL output. If you specify 0,1, it generates 0 or 1. For date values, enclose the parameter syntax in pound signs, for example, #<%Date%># |
Stored Procedures
You can use stored procedures with parameters in ActiveReports. The SQL query has the stored procedure call and placeholders for the parameters: CustOrderHist <%ID | Enter Customer ID | AFLKI%>.ActiveReports replaces the parameter with what the user types into the dialog to create a query like this: CustOrderHist 'AFLKI'.
You can add, edit, and delete parameters at run time. The following code demonstrates how to add a parameter and display its value in an ActiveReports textbox control.
To add parameters at run time in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code |
---|---|
Dim myParam1 As New Parameter() myParam1.Key = "myParam1" myParam1.Type = Parameter.DataType.String myParam1.PromptUser = True 'set to False if you do not want input from user myParam1.Prompt = "Enter last name:" myParam1.DefaultValue = "This is myParam1 default value" Me.Parameters.Add(myParam1) |
Visual Basic.NET code. Paste INSIDE the FetchData event. |
Copy Code |
---|---|
'Set textbox text equal to the value of the parameter Me.txtParam1.Text = Me.Parameters("myParam1").Value |
To add parameters at run time in C#
C# code. Paste INSIDE the ReportStart event. |
Copy Code |
---|---|
Parameter myParam1 = new Parameter(); myParam1.Key = "myParam1"; myParam1.Type = Parameter.DataType.String; myParam1.PromptUser = true; //set to false if you do not want input from user myParam1.Prompt = "Enter last name:"; this.Parameters.Add(myParam1); |
C# code. Paste INSIDE the FetchData event. |
Copy Code |
---|---|
//Set textbox text equal to the value of the parameter this.txtParam1.Text = this.Parameters["myParam1"].Value; |
To use parameters with subreports
Add a parameter to the datasource of the subreport as above, either in the sql statement or in code, and ensure that the parameter value exists in the parent report's data. the parameter is passed automatically. The only other consideration is that you need to set the ShowParameterUI property of the subreport to False to prevent the subreport from requesting the parameter value from the user.
You can use parameters with subreports to connect the subreport to the parent report. If you set a parameter for the field that links the parent report to the child subreport, the parent report passes the information to the child through the parameters. Keep the following in mind when working with subreports and parameters:
Both report queries must contain the same field (so the main report must have a categoryID field and the subreport also must have a categoryID field.
To set a parameter in a chart data source
Caution: If you don't set the same ORDER in both SQL queries, that of the report and that of the chart, the chart data is not ordered. |
SQL Query. Paste in the Query field. |
Copy Code |
---|---|
SELECT * FROM Products WHERE CategoryID = <%CategoryID||1%> ORDER BY ProductName |
Note: To see the chart draw at design time when using parameters, provide a default value. Otherwise, you must run the project and display the report in the viewer in order to see the chart. |