Data Environment and data Report In Visual Basic 6.0

Visual basic allows you to add a database environment just the way you add a form. A data environment designer is an ActiveX designer tool provided by Visual basic.A data environment designer provide a interactive design time environment to access data from a database at run time.A data  environment enable to perform following task:

  1. Right-click at the project in the project explorer whence a pop-up menu appears .
  2. Click at the add option when a list of insert able objects is displayed.
  3. Choose data environment when one is added with a default  name –data can always change the name to your choice  by setting its properties in the property window. :

4. Data  environment opens a connection to the source database . however ,you will have to set it up manually . Let us change the name of the connection1 to MyConnection1  simply type the same in the (Name) property as shown below.

5. The connection object of the data environment component has a number of properties that can be set to provide different functionalities to the component.

6. You can connect to a desired database by the following step:

7. Right click at the MyConnection1 or whichever connection you have.The popmenu shown below appeare from where you can choose properties.

8.When you click on properties… a dialog box shown below.This dialog box helps you set the database link to his connection.It lists all the types of database drivers currently installed on your machine.A typical dialog box appeares as shown below:

9. Select the Driver Microsoft  Jet 4.0 OLE  DB Provider.
10. Next,click at the Connection tab of the dialog box.The Connection tab opens up as shown below:

11. Select the Microsoft Access database you wish to connect to by clicking at button place on the right side of the textbox in which you enter the name of the database.The database name is shown in the text box shown in figure above.
12.Now,you can test whether your connection was established correctly by clicking at Test Connection button as shown below:

13. If the connection could not be established you will get the message for the same.Now your application is ready to use this connection to interact with the database.

When the connection is create, we can create DataReports.

How you can create Data Reports:-

A database contains a number of objects like tables,queries.when you have created a connection to the desired database,you need to select the table in the database from which the data will be fetch in the report.To do this right click at your data environment object and click at Add Command as shown below:

2. A command object with the default name command1 is added to the data environment as shown below:

3.Change Command’s name to MyCommand1 in the properties window.
4. Set the command to obtain data from a table from the connection database.To do this right click at MyCommand1 and select properties.A dialog box will appear as shown below:

5. In this dialog box set the desired Database Object like Table from where you wish to get the,we will take  our data from a table-Table1.
6. Alternatively,you can write an SQL statement for this command object by clicking SQL statement option button.

7.In case of Table object table fields are shown in MyCommand1 command object as shown in figure below:

8.Now you are ready to design your report.To include a report in your apllication add one Data Report object in the Project Explorer right clicking at the project as shown below:

9. A data report form is added to the project and displayed for modification .Change the name of this data report to MyReport1 into the property box as shown below.

10.To bind it to the data source MyDataSource1,click at Data Source property of the report and select the data environment- MyDataSource as shown below.

11. Now, your report knows from where it has to fetch its data.To select the source object,set the DataMember property to MyCommand1 in the property box of the report as shown below:

12. Now, you are ready to create your report,for this there are two way in Visual Basic.
In First way,open the DataEnvironment1  and drag the field from this and drop on the Detail(Section1). Drag and drop the fields as you want. Now click open the project property box from Project menu.

Set the Strup Object to DataReport1.Now run the project to show the report.Output is shown below.

The second way, By adding a command button to a form and writing some code to open the report at its event click procedure.For this click open the Form1 to add a command button as shown below.

Double click at the command button to open its event procedure. Add the code for opening the report as shown below:

Click open the project property box from Project menu .Set the Strup Object to Form1.Now run the project From1 should be displayed.Click  at the command button to show the report.Output is given below:

Comments are closed.