Adding and Viewing Crystal Reports in VB.NET– Floyd Winters
1. Open a new or existing VB.NET project 2. Click Project, Add Windows Form, select Crystal Report. Enter an appropriate name in the Name box and click [Open]. If you have not registered Crystal Reports yet, you will be prompted to do so. You can skip this step. 3. In the Crystal Report Gallery dialog box, click the radio button for As a Blank Report and click [OK]; the new report is added to your project and the new blank report opens with Header, Detail, and Footer sections. 4. Select a database to use with your report: A. Right-click on the yellow database icon or Database Fields at the top of the Field Explorer in the left pane. B. Select Add/Remove Database from the shortcut menu. C. Select Database Files or Other Databases from the Database Expert dialog box. D. Browse and find your .mdb file and click [Open]. E. After selecting your database, click the expand [+] button, if necessary, in the Available Data Sources Window to show the tables in your database. F. Select the desired table to work with, then click the [>] button to place the table in the Selected tables Window. G. Click [OK] when done. (This will give you a snapshot of the data. When a Crystal Report is viewed, it will not reflect any changes to the dataset made after it is loaded. Note: all connections in Crystal Reports are separate and independent of VB.NET, thus if you already have VB forms connected to databases, they are not automatically connected in Crystal.) 5. Expand the Database Fields selection at the top of the Field Explorer; then expand the table that you added to show all the fields in the table. 6. Drag and drop the desired fields to the Detail area of the report. (The headings are automatically inserted in the Page Header section – rename or reformat the headings as necessary.) (Resize the fields and headings as necessary.) 7. Add desired formulas: right-click on Formula Fields item in the Field Explorer and select New from the shortcut menu. Enter an appropriate name in the Formula Name dialog box and click [OK]. 8. Use the formula editor to make your formula. (Drag and Drop works) and then click the Save and Close icon. 9. Expand the Formula Fields selection and select your new formula and drag it to the detail of the report. 10. To Insert a Summary/Total field: Right click on Running Total Fields item in the Field Explorer and click new from the shortcut menu. Select the Field to summarize and Type of summary click [OK] and answer any prompts. 11. Right-click the Page Footer area and select Insert from the shortcut menu, select Special Field, then select the desired fields such as Date or Page number. Drag the special field to the desired spot on the Page Footer. 12. To add a Header: Right-click the Page Header area and select Text Object from the shortcut menu. Enter an appropriate caption for the header. 13. Click the Save All icon. 14. To View your report, add a new form to your project. Give the new form a size of about 750 by 550. (This is just less than 800 x 600) 15. From the Toolbox, add the Crystal Report Viewer to the new form (it is usually at the very bottom of the toolbar). Expand the size of the View to cover most or all of the entire form. 16. Select the ReportSource property of the new CrystalReportViewer control, and then browse and select the report that you just created. 17. You can open the new report with from a button on another form with code such as: Private Sub btnCrystalSum_Click(ByVal sender As System.Object, ByVal e As... Dim objCrystalSummary As New frmCrystalSummary() objCrystalSummary.Show() End Sub
Note 1: If you change the path or folder name or file name of your report you may have to right-click on the Database Fields selection at the top of the Field Explorer and select Set Location from the shortcut menu and/or reset the ReportSource property of the CrystalReportViewer control.
Note 2: When the application is run, the standard viewer has buttons for First, Last, Previous and Next pages, as well as Print and Export. These can be turned on or off in code. You can also click the Toggle Group Tree button (expand/collapse subtotals) to turn off the left tree pane (or change the DisplayGroupTree property to False). Click the Export Report button in the Viewer to Export as a .pdf, .doc, .xls, or .rtf file. These can be viewed with the Process.Start command. Ex: Process.Start("PaySummary.pdf")
Note 3: You can prompt the user for input to add data or to filter the report by using a parameter field. You can add one by right clicking on the Parameter Fields link in the Field Explorer on the left. As with formula fields, you enter an appropriate name in the Name dialog box, then provide the parameter input type and other basic default information pertaining to your parameter. Parameters only work when they are placed on the report or used in a formula (like a selection formula for filtering).
You can also do Subtotals for records with the same ID, if you Group the report: Right-Click, Insert Group and choose the ID field and the Set Running Totals.
To set your margins – Right-Click Form, choose Designer, and choose Page Set up
Exporting: When displaying the Crystal Report in Run time, you can click the Export Report icon (the Envelope icon) to export that .rpt as a .PDF, .XLS, .DOC, or .RTF file.
Remember that Crystal Reports is a Snapshot of when you captured the data, but there is a Refresh button.
Hints: How to Do a Form with only One Record per Page.
1. Place both Header and Fields in the detail section side by side. (Delete the field labels that appear in the header section) Ex: Name: Al Hours: 40 Rate: 10 Pay: $400 2 Right-Click the Form, choose Format Section, choose Details, and click the New Page After checkbox to only print one page per record.
TROUBLESHOOTING: If your database cannot connect to the Crystal Report when you move to a new location – 1. Open the Crystal.rpt 2. Right-Click on the Database Fields object in the upper-left corner of the Field Explorer 3. Choose Verify Database from the shortcut menu, then browse to the new location
|