Subreports Datasource


I am using a crystal report designer imported into VB from an existing report, loading in crystal viewer.
My report uses sub reports. I am setting the reports datasource at rumtime, how can i set the datasource of the subreport at runtime?
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.

Hi feesu,

If you could post some information around which version of both Crystal and VB you are running we probably supply some more definite answers, but until then check out this knowledgeBase entry to see if that helps:

Good Luck!
zephyr_hex (Megan)DeveloperCommented:
link your subreport to your datasource.  you do this in the main report.  right-click on the subreport and set the link to your datasource there.
feesuAuthor Commented:
Hi dkDeveloper,
I am using crystal reports 9, VB 6..

Hi zephyr hex,
I wanted to link it at runtime by code.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

sorry - no experience with the older versions.

This isn't what you've asked, but I had tons of trouble linking tables together with multiple links.  The crystal example/documentation/help doesn't mention that the source and destination fields can be arrays.

Dim crTable1 As CRAXDRT.DatabaseTable
Dim crTable2 As CRAXDRT.DatabaseTable
Dim crDatabaseSourceField1(1) As CRAXDRT.DatabaseFieldDefinition
Dim crDatabaseDestField1(1) As CRAXDRT.DatabaseFieldDefinition

Set crTable1 = crReport.Database.Tables(iTable1)
Set crTable2 = crReport.Database.Tables(iTable2)

Set crDatabaseSourceField1(0) = crTable1.Fields(sSourceField1)
Set crDatabaseDestField1(0) = crTable2.Fields(sDestField1)

Set crDatabaseSourceField1(1) = crTable1.Fields(sSourceField2)
Set crDatabaseDestField1(1) = crTable2.Fields(sDestField2)

crReport.Database.Links.Add crTable1, crTable2, crDatabaseSourceField1, crDatabaseDestField1, crJTLeftOuter, crLTLookupParallel, False, True

I never tried to create a sub-report via code.
By the way, I don't think there are many differences between the old and new via code.  Or, at least I don't remember any.  I wrote with CR8.5 and moved to XI just before deploying.
feesu, I didn't use VB6 for a long time and I can only provide you the sample in .net, C#, I think crystal report will provide the same method for VB6 as well. just give it a try and convert the code your own. hope it helps, good luck.

DataTable dtTable;
ReportDocument rptDoc;

... get data into dtTable....;
rptDoc.OpenSubreport("Sub-Report Name").Database.Tables["tablename or table number"].SetDataSource(dtTable);

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
feesuAuthor Commented:
Thanks you everyone for the help.

The code you sent worked in vb6. But what do you put in the sub report name space? full path or a variable representing the report object?
feesu, just put the sub-report name will do. Every sub-report must have a name when you create it. To change your sub-report name, right click your subreport->Format subreport->subreport->subreport name.
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.