You are on page 1of 14

UNIT 5 - ADVANCED CONTROLS

Data Access Control


 Data Access Control is a feature of Visual Basic that allows you to access and manipulate any
database from Visual Basic.
 The database may be either MS-Access database or FoxPro database or it may also be any of
the relational databases such as Oracle. You can develop applications in Visual Basic that can
access data in a database.
 The database may be on the same machine as the application or it may be on database server
that is far away from Visual Basic application.
Data Controls
 Data Controls are used connect the Visual Basic Applications and Database to display, add,
modify, and delete data, from a database.
 To make the connection between a form and a database, one places a data control in the form
and set its properties to identify the name of the database that one wants to access.
 The Data control automatically handles a number of functions including adding new records,
editing and updating existing records.

Following are Two widely used Tools to connects database from Visual Basic Applications.
(i) Data Access Objects (DAO) - Data Controls
(ii) Active Data Object Data control (ADODC)

(i) Data Access Objects (DAO) - Data Controls


The Data control is used for database programming ie. used to create & access database, tables and to
modify the databases. This model uses Jet Engine and particularly suitable for MS-Access database.
It is not suitable for ODBC data sources such as Oracle and MS-SQL Server.

Figure : Data Control

The Two most important properties of the Data control are:

(i) DatabaseName, which specifies the database Name.


(ii) RecordSource, which specifies the Table Name.
Right Click the Data Control and update the DatabaseName & RecordSource through the Property
Window.

The Data Control Tool can be used to perform the following tasks:
1. Connect to a database.
2. Open a specified database Table.
3. Used to Search and Display information from the Table.
4. Add new records or update a Table.
6. Close the database.

Functions:
1. MoveFirst repositions the control to the first record.

2. MoveLast repositions the control to the last record.

3. MovePrevious repositions the control to the previous record.

4. MoveNext repositions the control to the next record.

5. AddNew A new record is added to the table. All fields are set to Null and this record is made the
current record.

6. Delete The current record is deleted from the table. This method must be immediately followed by one
of the Move methods because the current record is invalid after a Delete.

7. Update Saves the current contents of all bound tools

To access the data of STUDENT table of COLLEGE.MDB:


1. Place Data Control on the form.
2. Invoke properties window and select DatabaseName property and click on three dots displayed on the
right of the property.
3. Visual Basic invokes DatabaseName Dialog Box. Select COLLEGE.MDB file using the dialog box. The
file is normally placed in D:\Program Files\Microsoft Visual Studio\VB98\ COLLEGE.MDB. However, the
exact path of this file may be different on your system.
4. The complete path of the file is placed in Properties windows.
5. Select RecordSource property and click on down arrow to the right of the property.
6. Visual Basic displays the list of tables from the selected database – COLLEGE.MDB.
7. Select STUDENT table from the list of tables.
8. Change Caption property to “Author Details”
Figure : Data Control With MS Flex Grid Control

Discuss about MSFlexGrid control.

 The MSFlexGrid control is an extremely useful tool for displaying information in a


tabular form.
 MS flex grid looks similar to a spreadsheet application . It contains rows and
columns.
 The box where a row and column intersect is known as a cell.
 MS FlexGrid control displays Information in Cells.
 It allows complete flexibility to sort, merge and format tables contains data.
 When you bound to a Data Control, MSFlex Gird displays read-only data.
 The Row and Col properties specify the current cell in a MSFlex Grid.

Add the MSFlexGrid control to your project:


1. Go to the Project menu and Click the Components.
2. Scroll down the list of components until you find "Microsoft FlexGrid Control 6.0
(sp3)". Select that checkbox and hit the OK button to add the control to your
project.
MSFlexGrid Properties Settings:

1. To make the connection between a MSFlex Grid and a Database, right click on the
MSFlex Grid control and select DataSource Name in the MSFlex Grid properties dialog
box.

2.From 'Property Pages' window, go to 'General' tab and go to 'Rows:' Textbox for
increase or decrease row cell.

3.Go to 'Column:' Textbox for increase or decrease Column cell. You can also adjust
Fixed Rows and Columns like it.

4.Just go to 'Fixed Rows:' Textbox and input row number and go to 'Fixed Cols:'
Textbox and input column number according to requirement.
Figure : Data Control With MS Flex Grid Control

Coding for Data Control With MS Flex Grid Control:


Private Sub Command1_Click()
Data1.Recordset.AddNew
End Sub

Private Sub Command2_Click()


If (Text1.Text = "") Or (Text2.Text = "") Then
MsgBox "Fill the required Information"
Else
Data1.Recordset.Update
MsgBox "Data is Saved"
End If
Data1.Refresh
End Sub

Private Sub Command3_Click()


Data1.Recordset.MoveFirst
End Sub

Private Sub Command4_Click()


Data1.Recordset.MoveLast
End Sub

Private Sub Command5_Click()


Data1.RecordSource = "select * from Student where Name = '" + Text3.Text + "'"
Data1.Refresh
End Sub
Private Sub Form_Load()
Form1.WindowState = 2
Text1.Text = ""
End Sub

(ii) Active Data Object Data control (ADODC)


 Microsoft has introduced a new object model called ActiveX Data Objects (ADO), which is based
on ActiveX technology, for the first time in Visual Basic 6.0.
 ADO Control uses OLE DB providers to connect the database.
 So, ActiveX Data Objects (ADO) Data Control is an tool to access relational database or non-
relational database from both Microsoft and other database providers such as Oracle, SQL, etc.
 So, We can connect Visual Basic Application and any other database through ActiveX Data
Objects (ADO) Data Control.

Add the ADO Data Control to your project:


1. Go to the Project menu and Click the Components.
2. Scroll down the list of components until you find " Microsoft ADO Data Control 6.0 (OLEDB)".
Select that checkbox and hit the OK button to add the control to your project as show in below
figure:

3. To make the connection between a ADOC Control and a Database, right click on the ADO data
control and select the ADODC properties, the following dialog box will appear, the Select
Microsoft.Jet.OLEDB 4.0 in Use Connection String Field.
Select the following Information as show in below image:

i) In this dialog box, select the database file you have created, Press test connection to see
whether the connection is successful. If the connection is successful, click OK to return to
the ADODC property pages dialog box.

ii) At the ADODC property pages dialog box, click on the Recordsource tab and select 2-
adCmdTable under command type and select book as the table name, then click OK.

Finally you need to display the data in the DataGrid control. To accomplish this, go to the
properties window and set the DataSource property of the DataGrid to Adodc1. You can also

permit the user to add and edit your records by setting the AllowUpdate property to True. If

you set this property to false, the user cannot edit the records.

Discuss about Data Grid Control in Microsoft Visual Basic 6.0.


 DataGrid control displays a series of rows and columns representing records
and fields from a Record set of a Database.
 It allows users to view and edit the data.

To add the DataGrid control in Visual Basic Form:


Step 1: Click on the Project in the menu bar and select Components where a dialog
box that displays all the available VB6 components.

Step 2 : Select Microsoft DataGrid Control 6.0 by clicking the checkbox beside this item.

Step 3: You also need to select the Microsoft ADO data control so that you are able to
access the database.

Step 4: Now you should be able to see that the DataGrid control and the ADO data
control are added to the toolbox. The next step is to drag the DataGrid control and the
ADO data control into the form.

Step 5: Now you need to connect the database to the ADO data control. To do that,
right click on the ADO data control and select the ADODC properties, the following
dialog box will appear.
Next click on the Build button and the Data Link Properties dialog box will appear (as shown
below). In this dialog box, select the database file you have created, in my case, the file
name is books.mdb. Press test connection to see whether the connection is successful. If
the connection is successful, click OK to return to the ADODC property pages dialog box. At
the ADODC property pages dialog box, click on the Recordsource tab and select 2-
adCmdTable under command type and select book as the table name, then click OK.

Finally you need to display the data in the DataGrid control. To accomplish this, go to the

properties window and set the DataSource property of the DataGrid to Adodc1. You can also

permit the user to add and edit your records by setting the AllowUpdate property to True. If

you set this property to false, the user cannot edit the records.

Data Grid Control Forms & Coding’s:


Private Sub Command1_Click()
Adodc1.RecordSource = "select * from Student_List"
Adodc1.Refresh
End Sub

Private Sub Command2_Click()


Adodc1.RecordSource = "select * from Student_List where Name like '" & Text1 & "'"
Adodc1.Refresh
End Sub
Private Sub Command3_Click()
Adodc1.Recordset.AddNew
End Sub

Private Sub Command4_Click()


Adodc1.Recordset.Fields("Name") = Text2.Text
Adodc1.Recordset.Fields("Reg_No") = Text3.Text
Adodc1.Recordset.Update
MsgBox ("Updated")
End Sub

Private Sub Form_Load()


Form1.WindowState = 2
SSTab1.ForeColor = vbBlue
Adodc1.Refresh
End Sub

Figure: ADODC Data Control & Data Grid Control


Discussed about MDI Form in Visual Basic 6.0.

 MDI stand for Multiple Document Interface. MDI forms are the special type of
forms, especially used to handle multiple child forms.

 A Multiple Document Interface is used to open many Forms at the same time.

 Visual Basic applications can have only one MDI form (Parent Form), which
contains all the child forms.

 A child form is an ordinary Form that has its Child property set True. Child form
are display within the internal area of MDI form at run time.

Add new MDI form to Project:

1. Open New project

2. Add MDI Form from Project Menu

3. Add Form from Project Menu

4. To form1 and Form2 set MDIChild property to True in Property of Form1 & Form 2

5. Create the Menu for Form1 and Form1

Restrictions of the MDI Form

1. You can have only one MDI form per project.

2. You can't place most controls on an MDI form. The only controls that can be placed

on the surface of the MDI form are Menus, Timer, CommonDialog, PictureBox,ToolBar,

and StatusBar.
Figure : MDI Form

MDI Form Coding:


Private Sub F1_Click()
Form1.Show
End Sub

Private Sub F2_Click()


Form2.Show
End Sub

Private Sub MDIForm_Load()


MDIForm1.WindowState = 2
End Sub

Discuss about RichTextBox Control in Visual Basic 6.0.

 The RichTextBox Control allows the user to enter and edit text, providing more
advanced formatting features than the conventional textbox control.
 You can use different fonts for different text sections.
 This control is loaded by selecting the Microsoft Rich Textbox Control 6.0 from
the Components dialog box.

How to Add the Microsoft Rich Textbox Control 6.0 control to your project?
1. Go to the Project menu and Click the Components.
2. Scroll down the list of components until you find "Microsoft Rich Textbox Control 6.0".
Select that checkbox and hit the OK button to add the control to your project.
Properties of the Rich Textbox Control:
1. LoadFile: It is used to Open a file and load the contents into the control.
2. SaveFile: It is used to Save the Rich Textbox contents into a file.
3. FileName: It is used to load the contents of a .txt or .rtf file into Rich Textbox
Control.
4. SelFontName: It is used to Set the font name for the selected text.
5. SelFontSize: It is used to Set the font size for the selected text.
6. SelFontColor: It is used to Set the font color for the selected text.

RichText Box Form Design


RichText Box Form Coding’s:

Private Sub Command1_Click()


cdb.ShowOpen
MsgBox cdb.FileName
RichTextBox1.LoadFile cdb.FileName
End Sub

Private Sub Command2_Click()


cdb.ShowSave
RichTextBox1.SaveFile cdb.FileName
MsgBox ("File is Saved")
End Sub

Private Sub Command3_Click()


RichTextBox1.SelPrint Printer.hDC
End Sub

Private Sub Command4_Click()


cdb.Flags = &H3 Or H100
cdb.ShowFont
RichTextBox1.SelBold = cdb.FontBold
RichTextBox1.SelFontName = cdb.FontName
RichTextBox1.SelFontSize = cdb.FontSize
End Sub

You might also like