Sie sind auf Seite 1von 5

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012

LECCION 10.2 Agregando campos calculados


Problem

I have a report that generates a list of items. I need to have the report count the number of items in the list.

Technique

Using the Data Report designer included with Visual Basic 6, you can create reports that can do various calculations based on the data in the report.

This How-To will demonstrate how to make a group based on the Authors field and add a calculated field that will display the number of books each author has written. This How-To will be a base demonstration how to add calculated fields to a Data Report.

Steps

Open and run DataProject.vbp, which displays the Figure 9.7. This displays each author's list of book and a count of the number of books per author. By using the toolbar at the top of the report you can print or export the report. 1. Start Visual Basic and select a new Data Project. This will add the Data Environment and Data Report to the project. Remove the form that was added to the project and set the Data Report as the Startup object. 2. From the Project menu select the Add Data Environment. Double-click on the DataEnvironment1 object that was added to the Project window. Right-click on the Connection folder and select the Add Connection item from the popup menu. Right-click on the Connection1 object and select the Properties item from the popup menu. Select the Microsoft Jet 3.51

OLE DB provider. The next dialog box that appears is used to find the BIBLIO.MDB file on your computer. Click the OK button. Right-click on the Command folder and select the Add Command item from the popup menu. Command1 will be added under the Commands folder. Right click on Command1 and select the Properties from the popup menu. Using the Data Object dropdown select the Tables item. Using the Object Name dropdown select the Titles item. Leccion 10.2 Agregando campos calculados Pgina 1

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012


Steps 1 and 2 were to set up the Data Environment. Please refer to the chapters in this book for a more detail on the Data Environment. 3. Under the Grouping tab, check the Group Command Object checkbox. This will enable the items in the tab. Click on the Author field and click the ">" button. This will cause the Data Environment to group the data by Author. Click the OK button.

4. Set the DataMember and DataSource of the Data Report. Right-click on the bottom of the report and select the Retrieve Structure item from the popup menu. This will arrange the sections of the Data Report to match the data used. 5. Place the controls on the data report form. Set the properties of the controls as in Table 9.7.

Table 9.7. Objects and properties for drBasic.dsr. OBJECT Property Setting Pgina 2

Leccion 10.2 Agregando campos calculados

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012


DataReport Name DataMember RptLabel Name Font FontSize Caption RptText Name Font FontSize DataField DataMember RptLabel Name Caption RptLabel Name Caption RptText Name DataField DataMember RptText Name DataField DataMember RptFunction Name DataField DataMember FunctionType RptLabel Name DataReport1 Command1_Grouping lbTitle Arial 14.25 Number of Books per Author txtAuthor Arial 12 Author Command1_Groping Label2 Title: Label3of Year Published: txtTitle Title Command1 txtYearPublished Year Published Command1 Function1 Title Command1 4 rptFunRCnt lbTotalBooks

Leccion 10.2 Agregando campos calculados

Pgina 3

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012


Caption Caption RptLabel Name Caption RptLabel1 Name Caption RptLabel Name Caption Total Books by Author %p lbPageTitle Page lbof of Label4 %P

6. Run the project. It might take a few minutes to retrieve the data from the database.

How It Works.

The report retrieves the data from the Data Environment. The Data Environment queries the database and groups the data by author. The rptFunction control that has been placed in the Group footer displays the total number of books per author. The rptFunction control can do various functions as in Table 9.8.

Table 9.8. rptFunction control functions description.

FUNCTION Sum Average Minimum Maximum Row Count

Setting 0 rptFuncSum 1 rptFuncAve 2 rptFuncMin 3 rptFuncMax 4 rptFuncRCnt

Description Adds the values of the field Averages the values of the field Displays the minimum value of the field Displays the maximum value of the field Displays the number of rows in a section Pgina 4

Leccion 10.2 Agregando campos calculados

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012

Value Count Standard Deviation Standard Error

5 rptFuncVCnt 6 rptFuncSDEV 7 rptFuncSERR

Display the number of rows containing non-null values Display the standard deviation Display the standard error

The rptFunction can be used to do various calculations based on the type of data in the field. By placing the rptFunction control in either a group or report footer, you can perform calculations on fields contained in the report.

Comments Grouping is a very powerful function when generating reports. Grouping enables the report to list similar items together in the report. For Example, the books were not added sequentially in the database, so when retrieving a list of authors and their books from the database all the authors are mixed up in the list. Grouping allows report to present all the books for one author then another. Another example is when creating a report to list the books per publisher. The grouping would be done on publishers not authors.

Leccion 10.2 Agregando campos calculados

Pgina 5

Das könnte Ihnen auch gefallen