Solved

Crystal Reports 7 RecordSortFields

Posted on 2000-02-23
2
1,738 Views
Last Modified: 2008-02-20
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
Comment
Question by:CrazyFox
2 Comments
 
LVL 1

Accepted Solution

by:
sramkris earned 70 total points
ID: 2551369
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
 

Author Comment

by:CrazyFox
ID: 2551438
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

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

762 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

20 Experts available now in Live!

Get 1:1 Help Now