You are on page 1of 17

Visual Data Tools

? Visual Data Tools


? Introduction Visual Basic is a powerful tool to enhance and display your data when you are working with databases. One can create basic custom database applications with just a few lines of program code, as VB implements the same database technology that is included with MS access (a database engine called MS Jet). The figure below is a roadmap of data access technologies found in Visual Basic.

The details of the above picture are explained in the slides. A customised database application is a program that takes the fields and records of a database and displays them in a way that is meaningful to a specific group of users. For example, a public library might create a customised version of its card catalog for a group of scientific researchers. Customised database applications typically present a variety of commands to their users. The commands allow users to search for, add, print, delete or modify records. Visual Basic 6.0 is having following Visual Data Tools to access the data from various databases like Dbase, Access, Oracle, SQL Server etc. on the form : 1) 2) 3) 4) ADO Data Control Data Environment Designer Data Grid MSHFlexGrid control

? ADO Data Control (Used to create bound forms) The bridge between the data providers and data consumers is through data sources created using Microsoft ActiveX Data Objects (ADO), which is the primary method in Visual Basic to access data in any data source, both relational and non-relational. For backward compatibility and project maintenance, Remote Data Objects (RDO) and Data Access Objects (DAO) are still supported.
Training Division, NIC, New Delhi

G 1

Visual Data Tools

? Difference between DAO, RDO, ADO


DAO : The data access objects(DAO) paradigm was the first object oriented interface that allowed programmers to manipulate the Microsoft jet database engine. The jet database engine is a technology to used to access the fields and records in Microsoft Access tables and other database sources. DAO is still popular and effective for single system database applications and small-size workgroup networks. RDO : The Remote Data Object(RDO) paradigm is an object-oriented interface to Open Database Connectivity(ODBC) sources. RDO is the object modal used by most database developers who work intensively with Microsoft SQL Server, Oracle, and other large relational database. ADO : The ActiveX Data Objects(ADO) paradigm has been designed as successor to DAO and RDO, and it has a similar object modal. In ADO, programmable objects represents all the local and remote data source available to your computer. Using new ADO control, by binding data objects to intrinsic and ActiveX controls, by creating DHTML application, and by using the new Data Environment Designer, can access these data objects. Most objects we create by using VB toolbox controls have the built- in ability to display database information. These objects are called bound controls. An object is bound to a database when its DataSource property is set to a valid database name and its Datafield property is set to a valid table in the database.

? To add ADO Data Control? to the form


Create a database (Employee) with a table(emp), whose structure is displayed in Fig 1 with few records in MS-Access for the illustration,

Fig. 1 Now we will be using VB to create the front end interface as shown below and at the back end MS Access Database is being used.

By default VB toolbox contains Data Control which can be used to connect databases like Dbase, Access, Excel etc. whereas ADO Data Control is used to connect database based on OLE DB technology which includes SQL Server and databases accessed via ODBC in addition to the previous databases.

Training Division, NIC, New Delhi

G 2

Visual Data Tools

Fig. 2 We will create the following form given in Fig. 2, first and then add the other command buttons for the user. Let us see how it is done: ? ? ? ? ? In Visual Basic, open a new Standard Exe Project. Click Project>>Components. Select Microsoft ADO Data Control 6.0 (OLE DB)1 . Click OK. Following data object picture will be displayed on the tool box

? In the form that appears, put the labels, text boxes and the ADO data object from the VB Toolbox, as shown in Fig. 2

If erroe message of File in the Path not found or File not registerred then copy the file wrt that object in the system folder and then register it with the command >regscvr32 <filename.ocx>

Training Division, NIC, New Delhi

G 3

Visual Data Tools ? As shown in the form above, set the properties of these objects as follows: Object ADO Data Control Property Name Caption ConnectionString Value Adodc1 Employee Details Provider=Microsoft.Jet.OLED B.3.51;Persist Security Info=False;Data Source=C:\employee.mdb emp Remark

path of database to be accessed See Part-I of Annexure I Name of table in the database being accessed i.e. source of recordset See Part-II of Annexure I Name of the Data Control through which database is being accessed

RecordSource

Text1

Name DataSource

txtempcode Adodc1

Text2

Text3

Text4

Text5

Datafield Text Name DataSource Datafield Text Name DataSource Datafield Text Name DataSource Datafield Text Name DataSource Datafield Text

empcode (Empty) txtempname Adodc1 empname (Empty) txtbasic Adodc1 basic (Empty) txtdesig Adodc1 desig (Empty) txtdepartment Adodc1 department (Empty)

-do-

-do-

-do-

-do-

Note: It may be noted that the five text boxes have the same DataSource Property but different field settings for the Datafield property.

Training Division, NIC, New Delhi

G 4

Visual Data Tools The five labels inserted in the form, should have the Caption property set to, Employee Code, Employee Name, Basic Pay, Designation, Department, respectively, as shown in the Fig. 2. ? Now you may save and run the form EmployeeDetails. The arrows in the data object allow you to navigate through the various records in the emp, as shown in Fig. 3.

Fig. 3 You may recall that we have used the RecordsetType to identify the database information as a table. In VB, a Recordset is an object representing the part of the database you are working with in the program. The Recordset object includes properties and methods that let you search for, sort, add and delete the records. We will now put the Find, Add , Delete and Quit buttons in the EmployeeDetails form to achieve the following screen.

Fig. 4

Training Division, NIC, New Delhi

G 5

Visual Data Tools ? To put the buttons of Find, Add, Delete and Quit, drag the button objects from the tool box to the Employee Details form. ? Giving the na mes as cmdFind, cmdAdd, cmdDelete and cmdQuit ,respectively to the above four buttons, you can type the codes for their respective event procedures as follows: Buttons Find Name Property cmdFind Code Private Sub cmdFind_Click()
prompt$ = "Enter the Employee Code" searchstr$ = InputBox(prompt$, "Search Box") Adodc1.Recordset.MoveFirst Adodc1.Recordset.Find "empcode = '" & searchstr$ & "'" If Adodc1.Recordset.RecordCount = 0 Then Adodc1.Recordset.MoveFirst End If

Add

End Sub cmdAdd Privat Private Sub cmdAdd_Click()


pr$ = "Enter the new record,and then click the left arrow button" reply = MsgBox(pr$, vbOKCancel, "add Record") If reply = vbOK Then txtempname.SetFocus Adodc1.Recordset.AddNew End if

Delete

End If End Sub cmdDelete Private Sub cmdDelete_Click() reply If prompt$ = "Do you really want to delete this Record?" reply= MsgBox(prompt$, vbOKCancel, "Delete Record") If reply =vbOK Then Adodc1.Recordset.Delete Adodc1.Recordset.MoveNext End If

Quit

cmdQuit

End Sub Private Sub cmdQuit_Click()


End

End Sub ? You may now run your program and try finding, adding and deleting a record.

Training Division, NIC, New Delhi

G 6

Visual Data Tools ? Data Environment Designer This feature allows you to quickly and easily create, design-time and run-time ADO objects. That is rather than having to create ADO objects, instantiate them, set their properties in code.

? Using Data Environment designer you can :


1. Create connection objects. 2. Create command objects based on stored procedures, tables, views, synonyms and SQL statements 3. Create hierarchy of commands based on grouping of command objects, or by relating one or more command objects together. 4. Write and run code for Connection and RecordSet objects. 5. Drag fields within a Command object from the Data Environment Designer onto a form or the Data Report Designer.

? Main features of Data Environment designer includes :


1. Data Environments are reusable : Once the object is set, they can be used over and over within the same project and they can be shared among the different projects. 2. Data Environments are scalable : It creates objects that are available both on client side and the middle tier.

? How to work with Data Environment Designer ?


For the illustration same MS-Access database created in ADO Data Control Object session will be in use. In addition to the emp table, create one more table called training which will contain the training details of an employee. Structure is given as below :

Add Data Environment Designer to the project ? Open the Standard Exe project. ? Click Project>>Add Data Environment. Following screen will appear :

Training Division, NIC, New Delhi

G 7

Visual Data Tools

? Right Click on Connection1 as indicated by arrow in the above figure. ? Click on Prope rties. Following screen will appear to select the OLE DB Provider :

? Click Next. ? Select Database name from the screen shown below :

Training Division, NIC, New Delhi

G 8

Visual Data Tools

? Click OK. ? Right Click on Connection1 again. ? Click on Add Command. Following screen will appear:

? Right Click on Command1 again. ? Click Properties. ? Select the Object Type and Name in the following screen :

Training Division, NIC, New Delhi

G 9

Visual Data Tools


? ?

Click OK. Drag Command1 on the form. Automatically bound fields will be created on the form. ? Later on buttons can be created for navigation, addition, deletion, search etc. Note : If data in the table is to be edited or added, set the LockType in the Advanced tab as Optimistic. Few code samples are given below : 1. For Moving to next record : DataEnvironment2.rsCommand1.MoveNext 2. For Moving to previous record : DataEnvironment2.rsCommand1.MovePrevious 3. For Moving to first record : DataEnvironment2.rsCommand1.Movefirst 4. For Moving to last record : DataEnvironment2.rsCommand1.Movelast 5. For Addition of Record: DataEnvironment2.rsCommand1.AddNew

Training Division, NIC, New Delhi

G 10

Visual Data Tools ? DataGrid Control The DataGrid control is a spreadsheet- like bound control that displays a series of rows and columns representing records and fields from a Recordset object. It is used to create an application that allows the end user to read and write to most databases. At the design time If DataGrid control is configured at design time then control is automatically filled and its column headers are automatically set from the data source's recordset. The grid's columns can be edited; delete, rearrange, add column headers to, or adjust any column's width. At run time The DataSource can be programmatically switched to view a different table, or you can modify the query of the current database to return a different set of records. Usage ? View and edit data on a remote or local database. ? Used in conjunction with another data-bound control, such as the DataList control, use the DataGrid control to display records from one table that are linked through a common field to another table displayed by the second data-bound control.

Training Division, NIC, New Delhi

G 11

Visual Data Tools

? How to Create Data Grid


DataEnvironment created in earlier is used here also. ? Create a new form and select Project >>Components. Check the Microsoft DataGrid Control 6.0 as shown below.

? Following data object picture will be displayed in the tool box. ? Using this toolbox object, create a data grid on the form. ? Set the datasource property of the grid control to the DE (i.e. dataenvironment1) and set the DataMember property to the appropriate command object (i.e. command1) as shown below:

Training Division, NIC, New Delhi

G 12

Visual Data Tools Now right click on the DataGrid and select "Retrieve Fields" as shown below. This will fill in the grid columns

The DataGrid's Menu

The result of this is that every field that is in the recordset (from the command object) shows up in the grid as shown below.

when you run this form, the output will be appeared as

If you would like to hide some of the fields, for instance "empcode", use the grid's property window to set the visible property to False. From design time, right click and select the Properties menu item to bring up the Properties window for the Grid Control (see below) and uncheck the Visible property of Layout option to hide the column.

Training Division, NIC, New Delhi

G 13

Visual Data Tools

? Working with Parent-child relationship in Data Grid


In Data Environment , Create Command1 and Command2 having parent-child relationship as shown below.

Drag Command1 over a form and Delete MSHFlexGrid created from form. Create DataGrid over the same form and set its properties. DataSource DataEnvironment1 DataMember Command1 AllowAddNew True Click right mouse button and select Retrieve fields

Training Division, NIC, New Delhi

G 14

Visual Data Tools The form will be appear as :

Write the following code for the buttons and data grid: Private Sub Command1_Click() DataEnvironment1.rsCommand1.MoveNext End Sub Private Sub Command2_Click() DataEnvironment1.rsCommand1.MovePrevious End Sub Private Sub Command3_Click() DataEnvironment1.rsCommand1.MoveFirst End Sub Private Sub Command4_Click() DataEnvironment1.rsCommand1.MoveLast End Sub Private Sub Command5_Click() DataEnvironment1.rsCommand1.AddNew End Sub Private Sub Command6_Click() DataEnvironment1.rsCommand1.Update End Sub

Training Division, NIC, New Delhi

G 15

Visual Data Tools Private Sub Command7_Click() End End Sub Private Sub DataGrid1_GotFocus() DataEnvironment1.rsCommand1.Update DataEnvironment1.rsCommand1.MovePrevious DataEnvironment1.rsCommand1.MoveNext End Sub

? MSHFlexGrid Control It is similar to the Microsoft Data Bound grid (DataGrid) control, but with the distinct difference that the Hierarchical FlexGrid control does not allow the user to edit data bound to, or contained within, it. This control, therefore, allows you to display data to the user while ensuring that the original data remains secure and unchanged. It is also possible, however, to add cell-editing features to your Hierarchical FlexGrid control by combining it with a text box. The Hierarchical FlexGrid Control is a flexible read-only display mechanism for OLE DB data. Using the Data Environment as created already, Hierarchical FlexGrid Control is used to display the related sets of data in a single grid. Add a child command to command1created above by : ? Clicking right mouse button on Command1. ? Select Add child command option. ? Right click child command Command2, following screen will appear :

Training Division, NIC, New Delhi

G 16

Visual Data Tools

? In General tab, select the object type table and object name as training, which is the second table.

? Getting Started with the MSHFlexGrid Control


? ? ? ? ? In Visual Basic, open a Project. Click Project>>Components. Select Microsoft Hierarchial FlexGrid Control 6.0 (OLE DB). Click OK. Following data object picture will be displayed on the tool box

? In the form that appear, put MSHFlexGrid Control and set the properties as given in the following table Property Value Data Source DataEnvironment2 (name of the Data Environment) Data Member Command1 ( name of the top- level command object to be displayed in the Grid) ? To make the control occupy the whole of the form area, write the following code in the Resize event of the form
Private Sub Form_Resize() MSHFlexGrid1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight End Sub

? Run the project . Results will be displayed as shown below :

Training Division, NIC, New Delhi

G 17

You might also like