Title: Data binding is the act of connecting a control in the programming environment directly to a field i
1Data Services Provided by VB
- Data binding is the act of connecting a control
in the programming environment directly to a
field in a row in a Recordset so that users can
directly view and edit the data by changing the
control contents. - ADO (ActiveX Data Objects) is the newest standard
for data access from MS programming environments.
2Objects in the ADO Object Model
- Connection object specifies information about
the physical connection with a data source. - Command object store actions performed on the
data, such as data modification and retrieval. - Recordset object provides a rich selection of
properties, events, and methods to expose data in
a field-row format.
3Setting Up the ADO Data Control
- Add the MS ADO DataControl 6.0 from the Project,
Components menu dialog box if the ADO Data
Control icon doesnt appear in the toolbox. - Place an instance of the ADO Data Control on the
form. - Specify the ConnectionString property for the
ADO Data Control. - Specify the RecordSource property for the ADO
Data Control.
4Binding VB Controls to the ADO Data Controls
Recordset
- Place a VB control on the form.
- Click on down arrow on the DataSource property
for the control to select the ADB Data Control. - Set the controls DataField property by
clicking the down arrow on the DataField
property to pick a field from the ADOs recordset.
5Set up a Connection in Code
- Set a reference to the latest ADO Library from
the Project, References menu dialog box. - Declare an object variable whose type is
ADODB.Connection. - Set the Connection objects ConnectString
property. - Call the Connection objects Open method.
- Dim cnIntro As ADODB.Connection
- Set cnIntro New ADODB.Connection '
Connection object - cnIntro.ConnectString Provider
Microsoft.Jet.OLEDB.3.51 - cnIntro.open
- Private sConnect As String
- sConnect DSN oracle UID ltyour idgt PWD
ltyour pwdgt - mobjDBConn.Open mDsnConnect
6Connection Object Events
- WillConnect monitor information about the
pending connection thats about to be opened to a
provider. - WillExecute examine and change the settings for
the action that will be executed on the provider. - BeginTransComplete, CommitTransComplete, and
RollbackTransComplete deal with database
transactions. - Disconnect perform post-connection cleanup.
7Set up a Recordset in Code
- Make sure you have a valid Connection or Command
object. - Declare an object variable whose type is
ADODB.Recordset. - Set the Recordset objects Source property.
- Call the Recordset objects Open method.
- Dim rsIntro As ADODB.Recordset
- Set rsIntro New ADODB.Recordset
- rsIntro.Source SELECT FROM lttable namegt
- Set rsIntro.ActiveConnection cnIntro
- rsIntro.Open
8Manipulate a Recordsets Data with Its Methods
- Every open Recordset must be associated with a
data cursor. - The five most common methods that enable you to
move the cursor Move, MoveFirst, MoveLast,
MoveNext, MovePrevious - Two most common methods that check on the cursor
BOF, EOF - Locate records the Find method takes up to four
arguments - Criterion (required) a string as the SQL where
clause - SkipRows a number representing the offset of
the starting position - searchDirection a flag indicating the direction
in which to search from the starting point - Start a double-type value that gives the
Bookmark of the record from which the search will
begin
9Manipulate a Recordsets Data with Its Methods
- Every open Recordset must be associated with a
data cursor. The five most common methods that
enable you to move the cursor Move, MoveFirst,
MoveLast, MoveNext, MovePrevious - Refer to Recordset field contents
- rsIntro.Field(2).value
- rsIntro.Fields(ltfieldnamegt).value
- rsIntro(ltfieldnamegt).value
- reIntro!ltfieldname
- Update a record assign values to fields first,
then call rsIntro.Update - Add a record rsIntro.AddNew then update a
record - Delete a record rsIntro.Delete
10Initialize Command Objects in Code
- Make sure your have a valid Connection.
- Declare an object variable whose type is
ADODB.Command. - Set the Command objects CommandType property.
- Set the Command objects CommandText property.
- Call the Connection objects Execute method.
- Set the Connection objects ActiveConnection
property to point to a valid existing Connection
object.
11Sample Code for Initializing Command Objects
Dim comIntro As ADODB.Command Set comIntro New
ADODB.Connection With comIntro Set
.ActiveConnection cnIntro .CommandType
adCmdText .CommandText sqlStr .Prepared
True Set rsIntro .Execute(sqlStr) End With