Visual Data Tools
Visual Data Tools
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
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.
G 2
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>
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.
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
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
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 ? You may now run your program and try finding, adding and deleting a record.
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.
Add Data Environment Designer to the project ? Open the Standard Exe project. ? Click Project>>Add Data Environment. Following screen will appear :
G 7
? 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 :
G 8
? 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 :
G 9
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
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.
G 11
? 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:
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 result of this is that every field that is in the recordset (from the command object) shows up in the grid as shown below.
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.
G 13
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
G 14
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
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 :
G 16
? In General tab, select the object type table and object name as training, which is the second table.
? 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
G 17