How to Change group Field at Runtime?

I want to change group field for crystal report at runtime through vb code. In my Database , there are six classes.When user select  a one class from the combo box in the interface , it should be grouped only for that selected class.

I tried like following

In the general declaration

Dim crpApplication As New CRPEAuto.Application
Dim crpReport As CRPEAuto.Report

add reference as
Crystal Report Enegine 7 Object Libeary

in the form load event

Set crpReport = crpApplication.OpenReport(App.Path & "\Reports\TestReport1.rpt")

also created a formula field named "Group1" and its index is 5 in the crystal report.also created a group for cClass1 there.

in the button click event of showing report

CrystalReport1.ReportFileName = App.Path & "\Reports" & "\TestReport1.rpt"

crpReport.FormulaFields.Item(5).Text = "{ItemMaster.cClass2}"

CrystalReport1.Action = 1

but it always show records only taking the default gruop (cClass1).

How can I solve this problem?

I have crystal report 7 version and but can also install a higher version if necessary.

send me mail to

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The method is relatively easy

crpReport.GroupCondition(0) = "Group1;{View.FieldName};CONDITION;SORTORDER"

{View.FieldName} - the crystal name for the field or formula to group on

CONDITION - A reserved word indicating how the grouping occurs.
  all other fields - ANYCHANGE

Sorry. Hit the wrong key.
SORTORDER - A ascending or D for descending

crpReport.GroupCondition(0) = "Group1;{ItemMaster.cClass2};ANYCHANGE;A"


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
show cclass1 is not what you select , am I right ?
if yes , means you never use DiscardSave of this report and this must put before action.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

I would recommnd you change and start using the RDC since the OCX has been dropped from CR9.

This should prove helpful in making the switch

Ido MilletProfessor of MIS at Penn State Erie and Owner, Millet SoftwareCommented:
Also, there are certain advantages to taking a slightly different approach.

In your report use a parameter to control a formula that returns different columns or expressions depending on the parameter value.  In your VB code, simply provide the parameter value.

- Ido

All of the solutions provided are good, but the easiest method I have used requires three steps.

1) Create an input paramter of some type (I usually use a string)

2) Create a function that contains a reference to the parameter (you may also need to include "WhileReadingRecords;".

3) Create a group using the function you just created.

Now when you are in your VB code and you select something from your drop down list, you simply pass that Item as an input parameter and you are good to go!
Glad I could help
Generally I don't ask but why the C?

Here instemprpt indicates Crystal report object....
Dim newGroupField As CrystalDecisions.CrystalReports.Engine.FieldDefinition
        newGroupField = instemprpt.Database.Tables(0).Fields(2)
        instemprpt.DataDefinition.Groups(0).ConditionField = newGroupField

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.