CONTROL ARRAY
CONTROL ARRAY
Using data control is a two-step process. You create the controls, such as labels
First you place a data control on a form and text boxes, to display the actual data.
and set the properties to link it to a Each control is a bound to particular field
database file and table in the table. In this example the label is
called a data bound control and
automatically displays the contents of
bound field when the project runs.
Data control generally links one form with If you want to have data-bound controls on
one table. second form, you must place a data
control on that form.
• This object model has very few objects and it is based on OLE DB interface.
OLE DB interface is a new interface (replacing ODBC and others), through
which you can access data of all formats in the same manner.
• ADO uses OLE DB providers to access the data. That means each database
is accessed through OLE DB provider. And ADO provides the programming
framework to access OLE DB. ADO is also much easier to deal with.
The data control can be used to
perform the following tasks:
• Connect to a database.
• 2. Open a specified database table.
• 3. Create a virtual table based on a database query.
• 4. Pass database fields to other Visual Basic tools, for
display or editing. Such tools are bound tools
(controls), or data aware.
• 5. Add new records or update a database.
• 6. Trap any errors that may occur while accessing data.
• 7. Close the database.
Data Control Properties:
• STEPS:
• 1. Open a new Visual Basic project.
•
• 2. Put a data control (an intrinsic control,
located in the VB toolbox) on the form and set
the properties as follows:
PROPERTIES OF DATA CONTROL
Property Value
(Name) datAuthors
DatabaseName ..\biblio.mdb
•
• datAuthors.Recordset.MoveNext
If datAuthors.Recordset.EOF =
True Then
datAuthors.Recordset.MoveLast
• End If
AddNew method of the recordset
object
• causes all the bound controls to be
cleared so that the user can enter data
ADDNEW CODE
• datAuthors.Recordset.AddNew
cmdSaveRecord.Enabled = True
• cmdMoveFirst.Enabled = False
cmdMoveLast.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
• cmdDeleteRecord.Enabled = False
cmdNewRecord.Enabled = False
SAVE BUTTON
• Private Sub Command2_Click()
On Error GoTo err1
Data1.Recordset.Update
MsgBox ("One Record is successfully Saved")
err1:
MsgBox ("Record Updated Successfully")
End Sub
Clear command
• Private Sub Command4_Click()
• Text1.Text = ""
• Text2.Text = “”
• End sub
Delete method
• On Error GoTo Delete_Error
•
• If MsgBox("Are you sure you want to delete this record?", _
• vbQuestion + vbYesNo + vbDefaultButton2, _
• "Confirm") = vbNo Then
• Exit Sub
• End If
•
• 'delete the current record
• datAuthors.Recordset.Delete
•
• 'move to a valid record
• cmdMoveNext_Click
•
• Exit Sub
•
• Delete_Error:
• ' This error will occur if you attempt to delete an author that is related to
• ' another table in the biblio.mdb database ...
• MsgBox "This record cannot be deleted. Error code = " _
• & Err.Number & vbCrLf & Err.Description, _
• vbCritical, "Cannot Delete"
DATABASE CONNECTION USING ADO
• ADO control. ADO stands for ActiveX data
objects. As ADO is ActiveX-based, it could
work in different platforms (different
computer systems) and different programming
languages.
• Error
• When an OLE DB provider error occurs during
the use of ADO, an Error object will be created
in the Errors collection. Other errors do not go
into an Error object, however. For instance,
any errors that occur when manipulating data
in a RecordSet or Field object are stored in a
Status property..
ADDING MICROSOFT ADO DATA CONTROL 6.0 (OLEDB)
COMPONENT