ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that...
-
date post
19-Dec-2015 -
Category
Documents
-
view
238 -
download
0
Transcript of ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that...
ActiveX Data Object
ISYS 562
ADO
• An ActiveX controlActiveX is build upon COM, a contract that defines a standard interface by which objects communicate.
• ADO works with several different DBMSs.– Can use ODBC DSN.
A Client/Server Access Application
Access Application Database ServerRequest
Response
Using ODBC
• Windows 2000/2003: • Control Panel /Administrative
Tools/DataSource(ODBC)
• Three types of data source names– System DSN
Using ADO
• Must reference ADO objects. In VBE:– Tools/Reference/
• click Microsoft ActiveX Data Object library 2.7
ADO Object Model
• There are three main objects on which the ADO object models is built:– Connection object: make connection to the
database.– Command object: run SQL against the
database.– Recordset object: holds some or all of the
records in database.
ADO Programming Object Model
Connection
Errors
Command
RecordSet
Error
Parameters Parameter
Fields Field
Connection with a Connection Object
• A connection object represents a unique session with a data source.
• Connection string: database, OLE DB provider, password, if any, security, etc.
• Use the Open method to open a connection.• Use the Execute method to execute SQL.• Manage transaction: BeginTrans,
CommitTrans, rollBackTrans.
Providers
• MSDASQL – Microsoft OLE DB Provider for ODBC
• Microsoft.Jet.OLEDB.4.0
• MSDAORA – For Oracle
• SQLOLEDB – Microsoft SQL Server
Connecting String
• Containing information about database, OLE DB provider, password, if any, security, etc.
• For Jet database:– Dim cn As ADODB.Connection
– Set cn = New ADODB.Connection
– cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;")
ODBC DSN Connection
• When using the ODBC provider, the connection string can be a DSN. For example:– Dim Cn as ADODB.Connection
– Set Cn = New ADODB.Connection
– cn.Open ("provider=msdasql;dsn=mySalesDB")
Connection Object Methods• OPEN
cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;")
Close• Execute:
– Return a recordset after executing a SQL Select statement.
– Usually is used to execute SQL Insert and Delete that don’t return set of records.
• Demo
Command Objects Properties & Methods
• Command object properties:– ActiveConnection: Current connection – CommandType: adCmdText, adCmdTable,
adCmdStoredProc, etc.– CommandText: Table name if command type is
adCmdTable, SQL statement if command type is adCmdText.
• Command object methods:– Execute
Creating ADO Recordset Objects
• Connection object’s Execute method
• Command object’s Execute method
• Recordset’s Open method
Types of Recordset
Creating Recordset Using ConnectionForwardOnly
Option Explicit
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;")
Set rs = cn.Execute("select * from customer", adCmdText)
Creating Recordset Using Command ForwardOnly
Option Explicit
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cm As ADODB.Command
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.MDB;")
Set cm = New ADODB.Command
cm.CommandType = adCmdText
cm.CommandText = "select * from customer“
Set cm.ActiveConnection = cn
Set rs = cm.Execute
Text1 = rs.Fields(0)
Text2 = rs.Fields(1)
End Sub
Creating Recordset Using Recordset’s Open Method
(Can open any type of recordset)
Dim cn As ADODB.ConnectionSet cn = New ADODB.Connectioncn.Open ("provider=msdasql;dsn=mySalesDB")Dim rs As ADODB.RecordsetSet rs = New ADODB.Recordsetrs.Open "customer", cn, adOpenKeyset ‘Table or SQLDim rc As IntegerDo While Not rs.EOFrc = rc + 1rs.MoveNextLoopMsgBox (rc)
Recordset Properties
• BOF, EOF• BookMark• CursorLocation• CursorType• LockType• MarshalOption• MaxRecords• RecordCount• Sort
Recordset Methods• AddNew, Update, Delete
• Find: Search a recordset.
• MoveFirst, MoveLast, MoveNext, MovePrevious
• Requery: Re-Execute the query.
• GetRows: Fetch records to an array.
Recordset Events
• FetchProgress, FetchComplete• WillChangeField, FieldChangeComplete• WillMove, MoveComplete• WillChangeRecord,
RecordChangeComplete• WillChangeRecordset,
RecordsetChangeComplete• EndOfRecordSet
Using SQL with ADO• SQL Select commands:
– Connection object’s Execute method
– Command object’s Execute method
– RecordSet object’s Open
• SQL Update commands: Insert, Delete, Update– Connection object’s Execute method
• Cn.Execute “Update EmpFile Set Salary = Salary*1.05”
– Command object’s Execute methodsqlSTr = " Update EmpFile Set Salary = Salary*1.05”cm.CommandType = adCmdTextcm.CommandText = sqlSTrcm.ActiveConnection = cncm.Execute
Private Sub Form_Load()Dim cn As ADODB.ConnectionSet cn = New ADODB.Connectioncn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;")'cn.Open ("provider=msdasql;dsn=mySalesDB")Dim rs As ADODB.RecordsetSet rs = New ADODB.Recordsetrs.Open "select cid from customer", cn, adOpenKeysetDo While Not rs.EOFList0.AddItem (rs.Fields("cid"))rs.MoveNextLoopEnd Sub
Private Sub List0_Click()Dim cn As ADODB.ConnectionSet cn = New ADODB.Connectioncn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;")'cn.Open ("provider=msdasql;dsn=mySalesDB")Dim rs As ADODB.RecordsetSet rs = New ADODB.Recordsetrs.Open "select cname from customer where cid = '" & List0 & "'", cn, adOpenKeysetMsgBox (rs.Fields(0))End Sub
• Dim rating As String• If Frame13 = 1 Then• rating = "A"• ElseIf Frame13 = 2 Then• rating = "B"• Else• rating = "C"• End If• db.Execute ("update customer set rating='" & rating & "'
where cid='" & List0 & "'")• Text9 = rating
Recordset’s AddNew
– A form with unbound textboxes is initialized for input.
– After entering data, a Save/Add button is clicked and the event handler uses a pair of AddNew and Update methods to insert record.
• Note: We can also use SQL Insert
Unbound Control Input ExamplePrivate Sub Command8_Click()Dim cn As ADODB.ConnectionSet cn = New ADODB.Connectioncn.Open ("provider=microsoft.jet.oledb.4.0;data source=c:\salesDB.mdb;")'cn.Open ("provider=msdasql;dsn=mySalesDB")Dim rs As ADODB.RecordsetSet rs = New ADODB.Recordsetrs.Open "customer", cn, adOpenDynamic, adLockOptimisticWith rs .AddNew .Fields("cid") = Text0 .Fields("cname") = Text2 .Fields("city") = Text4 .Fields("rating") = Text6 .UpdateEnd WithEnd Sub
Find Method
• Syntax: adoRecordsetName.Find criteria• Only accept simple comparison criteria.• Example:
– recordset.Find “CID = ‘” & txtCID & “’”
• Finding employees hired between 2/15/2000 and 5/15/2000:– Adodc1.recordset.Find “Hdate > #2/15/2000#”Then use IF statement in program to filter out those hired after 5/15/2000
Note: Search starts from the current position. To search from the beginning, must do: MoveFirst
If no match is found, the EOF of the recordset will be set to True.
Note: The search starts from the current position of the recordset. To start from the beginning, use the MoveFirst method.
Find Code Example
Dim searchID As StringsearchID = InputBox("enter searchID:")rs.MoveFirstrs.Find "cid='" & searchID & "'"If Not rs.EOF Then Text0 = rs.Fields("cid") Text2 = rs.Fields(1)Else MsgBox ("not found")End If
Demo
Using the BookMark
• Bookmark is a variant type property that keeps the current recordset’s position. This position can be saved in a variant variable and later go to that position.
BookMark Example
To save bookmark:
Dim BkMark As VariantPrivate Sub Command5_Click()
BkMark = rs.BookmarkEnd Sub
To go to the saved bookmark location:
Private Sub Command6_Click()rs.Bookmark = BkMarkText0 = rs.Fields("cid")Text2 = rs.Fields("cname")
End Sub