Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1829
  • Last Modified:

Crystal Reports 7 RecordSortFields

I'm using VB 6 and Crystal Reports 7.  I'm trying to set the report sort order at runtime based on user input.  
ie, If they select sort by Manufacturer Name then I'm trying to use the following code:

oCR.RecordSortFields(0) = "+{Return_ttx.MfgName}"

where oCR is a crystal report designer object

I get an error saying subscript is out of range.  Any suggestions on how to correct this?  Is there a better way to set the sort order at run time?
0
CrazyFox
Asked:
CrazyFox
1 Solution
 
sramkrisCommented:
Hi I hope this article helps you in sorting data as per your requirement

This article demonstrates how a report's sorting can be modified at runtime, using the Report Designer Component's (RDC) object model.


Solution

There are 2 scenarios in which a report's grouping can be modified at runtime.

1. Changing the existing sort's condition field.
2. Adding a new sort to the report.

NOTE:
- All the samples are based on the sample database, XTREME.MDB, which is installed by Seagate Crystal Reports.

1. Changing the Existing Sort's Condition Field

For this scenario, a report is created which is currently sorted on the Customer Name field (Ex. {Customer.Customer Name}). To change the condition field for the existing field at runtime, the following code can be used.

'General Declarations
Dim Report As New CrystalReport1
Dim crxDatabaseField As craxdrt.DatabaseFieldDefinition

Private Sub Form_Load()

'Currently the sort is based on the Customer Name field and the application
'is to change it to the 'Last Year's Sale's' field. This field must be present
'on the report. Basically, accessing the first table to get the 8th field
Set crxDatabaseField = Report.Database.Tables.Item(1).Fields.Item(8)

'There is only one Sort on this report so we will access the first item of the
'SortFields collection and set the field to the 'Last Year's Sale's' field
Report.RecordSortFields.Item(1).Field = crxDatabaseField

CRViewer1.ReportSource = Report
CRViewer1.ViewReport

End Sub

2. Adding a New SortField to a Report

'General Declarations
Dim Report As New CrystalReport1
Dim crxDatabaseField As craxdrt.DatabaseFieldDefinition

Private Sub Form_Load()

'Currently there is no sort in this Report. To add the Sort Field Customer Name
'the application must first get the {Customer.Customer Name} field from the Customer Table. Basically, 'accessing the first table to get the 2nd field
Set crpDatabaseField = Report.Database.Tables.Item(1).Fields.Item(2)

'Now add the field to the SortFields Collection and set the Sort Order to ascending.
Report.RecordSortFields.Add crxDatabaseField, crAscendingOrder

'Note. If the SortField is added while the Report is viewing the Viewer will have to be refreshed
'before the new sort will be active. This can be done by clicking the Refresh button in the Smart 'Viewer or Refreshing the viewer through code. Ex:
'CRViewer1.Refresh

CRViewer1.ReportSource = Report
CRViewer1.ViewReport

End Sub
0
 
CrazyFoxAuthor Commented:
sramkris,

Thanks a million, that worked like a charm.  I had no idea you had to use a CRAXDRT.DatabaseFieldDefinition to get that to work.  

Thanks again.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now