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:
- Right-click at the project in the project explorer whence a pop-up menu appears .
- Click at the add option when a list of insert able objects is displayed.
- Choose data environment when one is added with a default name –data environment1.you 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:
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:
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:
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 data.so,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.
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.
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: