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 Página 1

Leccion 10.2 - Agregando Campos Calculados

Embed Size (px)

Citation preview

8/2/2019 Leccion 10.2 - Agregando Campos Calculados

http://slidepdf.com/reader/full/leccion-102-agregando-campos-calculados 1/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 fieldthat 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 Página 1

8/2/2019 Leccion 10.2 - Agregando Campos Calculados

http://slidepdf.com/reader/full/leccion-102-agregando-campos-calculados 2/5

[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 inthe 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

Leccion 10.2 – Agregando campos calculados Página 2

8/2/2019 Leccion 10.2 - Agregando Campos Calculados

http://slidepdf.com/reader/full/leccion-102-agregando-campos-calculados 3/5

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012

DataReport Name DataReport1

DataMember Command1_Grouping

RptLabel Name lbTitle

Font Arial

FontSize 14.25

Caption “Number of Books per

Author”

RptText Name “txtAuthor”

Font Arial

FontSize 12

DataField Author

DataMember Command1_Groping

RptLabel Name Label2

Caption Title:

RptLabel Name Label3of  

Caption “Year Published:”

RptText Name txtTitle

DataField Title

DataMember Command1

RptText Name txtYearPublished

DataField “Year Published”

DataMember Command1

RptFunction Name Function1

DataField Title

DataMember Command1

FunctionType 4 – rptFunRCnt

RptLabel Name lbTotalBooks

Leccion 10.2 – Agregando campos calculados Página 3

8/2/2019 Leccion 10.2 - Agregando Campos Calculados

http://slidepdf.com/reader/full/leccion-102-agregando-campos-calculados 4/5

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012

Caption “Total Books by Author”

Caption “%p”

RptLabel Name lbPageTitle

Caption Page

RptLabel1 Name lbof  

Caption of  

RptLabel Name Label4

Caption “%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 andgroups 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 Setting Description

Sum 0 – rptFuncSum Adds the values of the fieldAverage 1 – rptFuncAve Averages the values of the field

Minimum 2 – rptFuncMin Displays the minimum value of the field

Maximum 3 – rptFuncMax Displays the maximum value of the field

Row Count 4 – rptFuncRCnt Displays the number of rows in a section

Leccion 10.2 – Agregando campos calculados Página 4

8/2/2019 Leccion 10.2 - Agregando Campos Calculados

http://slidepdf.com/reader/full/leccion-102-agregando-campos-calculados 5/5

[VISUAL BASIC CON BASE DE DATOS] IDSYSTEMS2012

Value Count 5 – rptFuncVCnt Display the number of rows containing non-null

values

Standard Deviation 6 – rptFuncSDEV Display the standard deviationStandard Error 7 – rptFuncSERR 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 Página 5