Creating reports is a main function of any good business application. You might have useful data in the system, but without a coherent way to present it, the numbers are meaningless. Visual Basic's primary reporting tool wasn't even a Microsoft product until this version. Previous versions of VB included a version of Seagate Software's Crystal Reports tool. With the release of VB 6, Microsoft has integrated a good report writer into VB that will provide most users an alternative to purchasing another tool.
The VB Data Report Designer will look familiar to developers who have built reports in Microsoft Access, because the layout of the tools is almost identical. However, the Data Report for VB can actually be compiled into your executable, which means no format files need to be installed on a user's machine. This also means that the format files can't be corrupted by users who decide to go exploring.
Your VB reporting workspace has a few new additions. The first is the Report Layout window (shown in Figure 31.1). You learn more about its different sections later in this example.
FIGURE 31.1 The Report Layout window looks similar to the Report Designer used in Microsoft Access.
The next change is the addition of six new controls to the Toolbox, as described in Table 31.1.
Data Report Designer ToolBox controlsThese six controls aren't interchangeable with their counterparts you've always used for form design. Likewise, you can't use form design controls in Data Report Designer.
Control Type | Description |
RptTextBox | Any data that will be supplied at runtime--through code or through a command--needs to be put in a RptTextBox. RptTextBox controls can be configured to look like plain text without any borders. |
RptLine |
Use this control to draw a variety of lines on the report. |
RptFunction | This control performs functions on data groups in the report and can be used only in a group footer section of the report. It supplies functions such as row and value counting, sums, and so on. |
RptLabel |
The RptLabel control is used to add static text to the report. |
RptImage |
Use the RptImage control to add a picture to your report. This control, like the standard Image control, can hold bitmaps (BMP), icons, metafiles, or GIF or JPEG files. |
RptShape |
You can use the RptShape control to add a variety of graphical shapes to your report, including rectangles, circles, and variations of them. |
As its name implies, the Data Report Designer builds reports from database tables. The example in this chapter uses the Northwind Traders database (Nwind.mdb), which is included in the main Visual Basic directory.
Start building your report
FIGURE 31.2 RptLabel controls enable you to add static text anywhere on your report. The text in the Report Header section repeats on every page in your report.
Cleaning up the reportYou can also set the Caption property of the Report itself by using the Properties window when the Report is selected. Also, any blank space you leave around the controls will be repeated whenever the report is shown, so be sure to place the controls correctly and to close up any blank space around them.
You can add other labels or graphics to the report as you want. The next step is to connect the report to a data source by using the Data Environment Designer. A data environment contains all the connections and queries that you're using in your application. In this example, you need a query to generate a list of customers for your report.
Using the Data Environment Designer
FIGURE 31.3 Always use the Jet OLE DB Provider when connecting to Access databases in a data environment.
FIGURE 31.4 Enter the filename of the database you want to use in this connection. Access always provides the Admin user ID, even if you don't specify one. For this example, you can leave Admin as the user ID.
You've successfully created a data environment. Be sure to save your work but don't shut the window just yet. With the data environment created, you can now create the query that will retrieve customer information from the database.
Create the query to retrieve information from the database
FIGURE 31.5 All the properties you need to set for this query are on the General page.
Use the SQL BuilderYou can use the SQL Builder if you want to create complex joins and don't want to type the SQL yourself.
SELECT CompanyName, City, Region, Country, PhoneFROM Customers ORDER BY CompanyName
Be sure to save your work because you have now finished building your data connections.
Finish building the report
Finding the Properties windowIf you can't see the Properties window (where these properties can be set), press F4 to open it.
Using repeating rowsThe Detail section is very narrow because any blank space in this section will be repeated for every row in the report. Also notice that a line is drawn beneath the column headers. You can use the RptLine control to create this graphic, if you like.
FIGURE 31.6 You can move your controls between sections to create a column-based report.
%p Current page number %P Total number of pages %d Current date (short format) %D Current date (long format) %t Current time (short format) %T Current time (long format) %i Report title
Page %p of %P
Now that the report is complete, you can run it just like any other module in your project. Choose Properties from the Project menu and use this report as your startup form. When you run your program, your report will be displayed as shown in Figure 31.7.
Centering the page numberTo center the page number, right-click the RptLabel, select Center in Section, and then select Horizontally from the pop-up menu.
FIGURE 31.7 The report is shown in its final form in run mode.
With the report viewer, users can print or export the report to plain text or HTML. As shown in Figure 31.7, you also can shrink or enlarge the report by using the Zoom feature available on the viewer.
To show this report from code, use the standard Show method. You can use the PrintReport method to instruct the report to print through code. Adding a True or False to the PrintReport call tells VB whether to display a Print dialog. You don't even have to have a CommonDialog control in your project--VB will automatically create the Print dialog.
© Copyright, Macmillan Computer Publishing. All rights reserved.