Solved

Crystal Reports 7 RecordSortFields

Posted on 2000-02-23
2
1,776 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

710 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