Creating a Data Environment Designer Using Data View

5.7. Generate reports


5.7.1. Create data links using Data View

The Data View window allows us to work with a database without using external tools or Add-ins. To use the Data View window, follow these steps:

Step 1. From the View menu, select Data View, or click the Data View button on the standard toolbar. The Data View window appears, which gives us two folders, Data Links and Data Environment Connections.

Maybe you are interested!

Step 2. In the Data View window, right-click on the Data Links folder.

From the pop-up menu, select Add a Data Link, the Data Link Properties window appears.


Step 3. Select the Microsoft Jet provider, then click Next. The Connection tab appears. Enter the path and file name of the database you want to use.

Step 4. Click the Test Connection button at the bottom of the window to test the connection to the database.

Step 5. Click OK, the data link is established, and the Data View window prompts us to enter a name for the link. Type in QTKD, then press Enter.

Data links provide a summary view of the data source. Once you create a data link, you can browse it using the elements in the summary list. You do this by clicking the plus sign to the left of each element. The database expands fully in the data view.

5.7.2. Create a data environment designer using Data View

We can create a Data Environment design to visually manage the connection to a database. When we have a Data Environment design, it is included in the application binary at compile time, so we don't have to worry about external dependencies. To add a Data Environment design to an application using the Data View window, follow these steps:

Step 1. In the Data View window, click the Add Data Environment button. The new Data Environment will appear in the project. A default connection, called Connection1, appears in the design.

It is possible to manually adjust the default connection in a Data Environment design so that it points to the database. But the database is already in the Data View window, we just need to drag and drop the table into the design. To do this, we do the following:

Start the Data view window, select a table in the Tables folder (such as tblDS_KH)

Drag the table onto the Data Environment design. A new connection called Connection2 appears in the design, with the table appearing below. At this point, we can drag other tables into the design as needed. When finished, we have:


5.7.3. Create data reports

Creating a report on a database is more than just displaying data from a database. Most database reports involve a number of other operations on the data such as:

Query data to return, display, and print the parts we want. This is called filtering. Sort data so that it appears in a meaningful order.

Group data to display it neatly.

There are many methods to create reports such as: Using Data Report design, using Microsoft Access to make reports, using Crystal report to create reports.

etc...

Here we use Data Report design.

The Data Report Designer is an intuitive way to create custom reports in the Visual Basic development environment. To create a report using Data Report, follow these steps:

Step 1. In the project, from the Project menu, select Add Data Report. The DataReport design is added to the project.

The Data Report contains several segments:

Report Header displays once at the top of the report.

Report Foodter is displayed once at the end of the report

Page Header displays at the top of each page

Page Footer displays at the bottom of each page

Detail section displays data rows

One or more group headers or footers appear at the beginning or end of a group segment.

The easiest way is to create a DataReport using the Data Environment design. With the Data Environment we can use drag and drop to design the report.

Every time we create a DataReport design, we must bind it to a database to display data. We do this through a set of bound controls that only work in the DataReport design context.

The visual controls of the DataReport design include:

Label control

Text box control

Image control

Line control and shape control

Function controls allow the insertion of summary calculations and reports Step 2. Create a Data Environment design

Step 3. In the Data Environment designer, select and drag a field from the Data Environment design to the Detail section of the DataReport design. A label control and a text box bound to the data field appear on the DataReport design. For example:

Step 4. Define the data source for the report: Select the DataReport design by selecting it in the drop-down list of the Properties window, in the Properties window, specify the DataSource property of the DataReport as the name of the Data Environment designer.

Step 5. Preview the report using the Show method. For example, if the DataReport is called drCustomer, we can run it using the command line:

drCustomer.Show

The report is displayed as shown below:

5.7.3. Create data reports using Command objects

a) Create database connection

Step 1: Create a new project

Step 2: To create a report, we must determine what data will be used in the report. We insert a Data Environment into the project by selecting ProjectAdd Data Environment. The Data Environment window appears:


Step 3: Right-click on Connection1 and select Rename then change its name to tk_muonsach; right-click on tk_sach_nhaxb and select Properties, the Properties dialog box appears:


Step 4: We will create a connection to the Qltv.mdb database in the QLTV folder. Therefore, we double-click on Microsoft Jet 4.0 OLE DB Provider because we use the Access database, the Data Link Properties dialog box appears, type the path to the qltv.mdb database in the QLTV folder. As shown below:

Step 5: Click the Test Connection button to check if the connection is successful or not? If successful, a message box will appear:

Step 6: Click the Ok button to close the message box, then click the OK button to close the Data Link Properties dialog box

b) Create Command object

Step 7: After defining the Data Environment Connection, we can define the Command, Command saves the connection to the data table and the fields that will be used in the report. Right-click on the blank area in the Data Environment window and select Add Command, a Command1 object appears in the tk_sach_nhaxb connection.

Step 8: Right-click on Command1, select Rename and rename it comnhaxb, select Connection as tk_sach_nhaxb, in the DataBase Object section select the data retrieval type as Table; then in the Object Name section select the sach table, as shown in the following image:

Click the OK button to close the comnhaxb Properties dialog box.

c) Create a simple report

Step 9: Now we will create a report to display the data defined in the Data Environment. To do this, right-click on the Project in the Project Explorer window; then select AddData Report; then rename this report to drnhaxb and set the values ​​for some of its properties as follows: DataSource = DataEnvironment1, DataMember = comnhaxb (in the Properties window). Next step we close all windows except the 2 windows DataEnvironment1 and drnhaxb.

Step 10: This step is simply clicking and dragging the fields from Data Environment, comnhaxb section to Data Report. For this report, click and hold comnhaxb and drag it to the Detail section of the drnhaxb report.

Step 11: When we drag and drop fields into the Detail section of the report, an RptLabel containing the field name and an RptTextBox allowing data entry appear in the Detail section. Therefore, we only leave the fields in the Detail section of the report, and drag the column headers (corresponding to the fields) to the Page Header section of the report, insert the RptShapes to draw rectangles to create a tabular report. Adjust the position, size, and font of the objects on the report accordingly.

Step 12: Add an RptLabel to the Header section of the report; then change its Caption property to LIST OF BOOKS; Font property is .vntimeH, size 16

Step 13: Right click on the report Footer item and select the Insert Control option

| Current Page Number to insert the page number; then select Insert Control | Total Number of Pages to insert the total number of pages of the report.

Step 14: At this point, the report design is complete, the repota now looks like this:

Step 15: Save the report and create an interface for the program to display the report.

Comment


Agree Privacy Policy *