Load and view Crystal Reports XI external RPT files.

Submitted on: 2/16/2015 6:54:00 PM
By: OASyS (from psc cd)  
Level: Beginner
User Rating: By 5 Users
Compatibility: VB 6.0
Views: 11545
     Allows you to load and view any outside XI report file from your VB6 code.


I had decided to publish the following code into PSC since I'm having looking for it for weeks and I had not found any similar code and explanation in whole Internet. The following code is really a compilation of dozens of manuals, codes, forum messages and so forth. I hope it could be usefull to you. If you agree, please vote...


1) You must enable the "Crystal ActiveX Report Viewer Library 11.0" in the CONTROLS tab of PROJECT - COMPONENTS. A new object will appear in the VB6 TOOLBOX section (the REPORT VIEWER ocx).

2) Create a new form named "FRMCRYSTAL" (frmCrystal.frm) and add the Crystal object into it. The report viewer window will appear inside your form. Put the following lines in its LOAD event:

CRViewer1.Top = 0
CRViewer1.Left = 0
CRViewer1.Height = ScaleHeight
CRViewer1.Width = ScaleWidth

Set its "VISIBLE" property to FALSE and close the form.

2) To open and display an external Crystal XI report, you may use these lines in any place of your program:
I'll divide the task in 4:

2.1) point and open the report;
2.2) change the report parameters;
2.3) change the report SQL query; and
2.4) view the report.

2.1)  Let's open the external file.The second parameter ("1") means NO-EXCLUSIVE open or TEMP-COPY open.
If you change for zero (EXCLUSIVE), the report will open just once per session.

Dim MyApp As New CRAXDRT.Application
Dim MyRpt As New CRAXDRT.Report

Set MyRpt = MyApp.OpenReport("c:\windows\sample.rpt", 1)

2.2) Let's change some parameters. Interesting to note the FORMULAFIELD syntax, where I can name my text-fields instead to utilize their indexes (easier to work on than the Business Solution's Crystal manual).  Obviously, the name between parenteses must reflect the exact formula/text/field name. In that example, the formulas are STRING type.

MyRpt.ReportTitle = "That's my Title!"

MyRpt.FormulaFields.GetItemByName("InitialDate").Text = "'Initial Day: " & dr1(0).Value & "' "
MyRpt.FormulaFields.GetItemByName("FinalDate").Text = "'Final Day: " & dr1(1).Value & "' "

2.3) Change the report SQL query...

x = 100 : y = 1000
MyRpt.SQLQueryString = "select * from dropouts where InitialIssue <= " & x & " and FinalIssue >= " & y

2.4) Finally, log on your server and database. Since your report already exist and has the appropriated fields and layout, you must use the same database and login info present in the report. In my example, you must change the names for your correct ones.

MyRpt.Database.Tables(1).SetLogOnInfo "<server>", "<database>", "<login>", "<password>"

' And voilá !!! Show the report!!!

frmCrystal.CRViewer1.ReportSource = MyRpt

' Clear memo...

Set MyRpt = Nothing
Set MyApp = Nothing


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.