Database
Database
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.
• The ADO Recordset object is used to hold a set of records from a database
table. A Recordset object consist of records and columns (fields). In ADO,
this object is the most important and the one used most often to
manipulate data from a database.
• 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:
• RecordSource Determines the table (or virtual table) the data control
is attached to.
DataSource
• The DataSource is the name of the data
control on the form (it should already be
configured), and the
DataField
• DataField is the name of the particular field in
the database that should be displayed in the
control (this field will be in the table that was
chosen for the RecordSource of the data
control).
DATABASE ACCESS WITH THE DATA CONTROL
• 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
• Open Method
– CN.open
– The open method accepts a number of
optional arguments (ConnString, UserID,
password, options)
• Close Method
– CN.Close
– Set CN = Nothing (remove the Connection
Object from memory)
Connection Example
Dim dbcon as ADODB.Connection
Set dbcon = New ADODB.Connection
dbcon.ConnectionString _
="Provider=MSDASQL.1;Persist Security _
Info=False;Data Source=NWIND"
dbcon.ConnectionTimeout = 10
dbcon.Open
dbcon.close
Set dbcon = Nothing
The Command Object
• Dim cn As ADODB.Connection
• Dim rs As ADODB.Recordset