Using Parameters in ActiveReports for .NET


04-23-2003, 3:38 PM
You can use the ActiveReports Parameters collection to pass parameters to the report's SQL string at run time. This can be done with the parameters dialog, which will prompt the user for input when reports are generated, or, if you're using parameters to synchronize subreports or passing the parameters in from other code, you can set the report's ShowParameterUI property to False.

The syntax for adding parameters to the report's Parameters collection via the SQL string is:

<%FieldName|PromptString|DefaultValue|Type%>

Example:

SELECT * FROM Products INNER JOIN Categories on Products.CategoryID = Categories.CategoryID WHERE Products.SupplierID = <%SupplierID|Enter supplierID|1000%> and OrderDate = #<%Date|Order date: |1/1/2001|D%># and Discount='<%bool|Is this checked?|True|B%>'

Passing Parameters to an ActiveReport

Once you have added parameters to your report's Parameters collection through the SQL statement, you can access them with code:

Dim rpt as New rptYourReport()
rpt.Parameters(0).Value = 1
'A variable which takes its value from your user interface may be passed in here
rpt.Parameters(1).Value = #4/1/2003#
rpt.Parameters(2).Value = 'False'
rpt.Run()

Stored Procedures and Parameters

Stored procedures can be used the same way as parameters in ActiveReports. The SQL statement would need to have the stored procedure call and placeholders for the parameters:

CustOrderHist '<%ID|Enter Customer ID: |AFLKI%>'

ActiveReports will then replace the parameter text "<%...%>" with whatever the user types into the dialog to create a call like this: CustOrderHist 'AFLKI'.

Parameters and Subreports

Parameters can be used with subreports to connect the subreport to the parent report. By setting a parameter for the field that links the parent report to the child subreport, the parent report can pass the information to the child through the parameters. The main differences when working with subreports and parameters are:

Note: Both main and subreport queries must contain the same field (so the main report must have a CategoryID field and the subreport also must have a CategoryID field.