Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming the RecordSet Object

Similar presentations


Presentation on theme: "Programming the RecordSet Object"— Presentation transcript:

1 Programming the RecordSet Object

2 Recordset Object (ADO)
When you use ADO, you manipulate data almost entirely using Recordset objects. A Recordset object represents the entire set of records from a database table or the results of an executed command. All Recordset objects are constructed using records (rows) and fields (columns).

3 Cursor A cursor caches data on a client.
Provides synchronization mechanisms Provides tools to minimize network traffic. Define how a cursor is created using the CursorLocation and CursorType properties pertaining to the Recordset object. You must set these properties before opening the recordset. cursor In database technology, a piece of software that returns rows of data to the requesting application. A cursor keeps track of the position in the result set, and multiple operations can be performed row by row against a result set with or without returning to the original table. In other words, cursors conceptually return a result set based on tables within the databases. The cursor is so named because it indicates the current position in the result set, just as the cursor on a computer screen indicates current position.

4 The Recordset CursorLocation Property
Specifies the location of the cursor engine and can be set to one of two constants: adUseClient adUseServer default If set to adUseClient, the program uses a local client cursor. Local client cursors tend to support additional functionality beyond that supported by server cursors. If set to adUseServer (the default), the program uses the cursor supplied with the data provider.

5 The Recordset CursorType Property
The CursorType property defines the kind of cursor that will be opened. adOpenStatic creates a static cursor adOpenForwardOnly creates a forward only cursor adOpenDynamic creates a dynamic cursor adOpenKeyset creates a keyset cursor The constant value adOpenStatic creates a static cursor. A static copy of the data is created and stored in the Recordset object. Changes made to this static copy do not become visible to other users. The constant value adOpenForwardOnly creates a cursor that is identical to a static cursor, but can only scroll forward through the records. In situations when you need to scroll through a group of records only once to print a report or perform a similar task, forward-only cursors represent the optimal choice because they improve performance. The constant value adOpenDynamic creates a dynamic cursor. Additions, changes, and deletions will then be valid and visible to other users. The constant value adOpenKeyset creates a keyset cursor. A keyset cursor is identical to a dynamic cursor, but records added by other users are not reflected in the open recordset. If a user deletes a record, that record is not accessible in your recordset. Changes to existing records made by other users are visible to your recordset.

6 The Recordset LockType Property
To resolve the conflicts that may arise when two users try to change the same records, database providers support locking. To specify the type of locking, set the LockType property to one of the following: adLockReadOnly (default) adLockPessimistic adLockOptimistic In multi-user environments, more than one user may access the same database simultaneously. Furthermore, multiple users may attempt to edit the same record or group of records at the same time. If the LockType property is set to adLockReadOnly, the provider creates a read-only recordset – that is, the contents of the recordset cannot be modified. If the LockType property is set to adLockPessimistic, the provider applies pessimistic locking. With pessimistic locking, a record is locked when editing begins and unlocked after the record is updated. If the LockType property is set to adLockOptimistic, the provider applies optimistic locking. With optimistic locking, the record remains locked only from the time the Update method is called to the time the update finishes.

7 The Recordset Open Method
The Open method opens a cursor (recordset). Syntax: recordset.Open Source recordset must be an instance of an ADO Recordset object. The Source argument is the name of a command object. It can also be an SQL statement or table name.

8 Private rstCurrent As New ADODB.Recordset
Private Sub Form_Load() Dim conMagnet As New ADODB.Connection Dim cmdMagnet As New ADODB.Command conMagnet.Mode = adModeShareDenyNone conMagnet.Provider = "Microsoft.Jet.OLEDB.4.0" conMagnet.ConnectionString = "Data Source=A:\Magnet.mdb" conMagnet.Open Set cmdMagnet.ActiveConnection = conMagnet cmdMagnet.CommandType = adCmdTable cmdMagnet.CommandText = "tblOrderMaster" rstCurrent.LockType = adLockOptimistic rstCurrent.CursorLocation = adUseClient rstCurrent.CursorType = adOpenKeyset rstCurrent.Open cmdMagnet Call LoadCurrentRecord End Sub

9 Recordset Navigation Methods
Navigate through the records in a Recordset with the following Recordset methods MoveFirst MoveLast MoveNext MovePrevious Move Examples: rstCurrent.Move +5 rstCurrent.MoveFirst rstCurrent.MoveLast MoveFirst and MoveLast methods move to the first and last records in the recordset. The MoveNext and MovePrevious methods move relative to the current record or bookmark. The Move method moves a specific number of rows forward or back. The examples show navigation statements to move through a Recordset object named rsCurrent

10 Recordset BOF and EOF Properties
The BOF and EOF properties indicate the beginning or end of the Recordset. The BOF or EOF property is True when you move one record past the first or last record. If both BOF and EOF are True, there are no records in the Recordset. Example: If rstCurrent.BOF = True Then rstCurrent.MoveFirst EndIf When you open a Recordset, the current record is positioned to the first record (if any) and the BOF and EOF properties are set to False. When you use MoveNext or MovePrevious, you should check the BOF and EOF properties to make sure you do not move off the recordset. The example checks for the beginning of a Recordset object named rsCurrent.

11 Recordset RecordCount Property
Use the RecordCount property to return the number of records in a Recordset object. The property returns -1 when ADO cannot determine the number of records. Reading the RecordCount property on a closed Recordset causes an error. Example: txtRecordCount.Text = rstCurrent.RecordCount

12 Recordset AbsolutePosition Property
Use the AbsolutePosition property to determine the current record number The AbsolutePosition property is a Long Integer between 1 and the number of records in the Recordset. Example: Use the AbsolutePosition property to move to a record based on its ordinal position in the Recordset object, or to determine the ordinal position of the current record. txtCurrentRecord.Text = rstCurrent.AbsolutePosition

13 Recordset Find Method recordsetName.Find (criteria) Searches a Recordset for the record that satisfies the specified criteria. If the criteria is met, the recordset position is set on the found record; otherwise, the position is set on the end of the recordset. Example: rstCurrent.Find “[Member ID] = 1234” Parameters criteria   A String containing a statement that specifies the column name, comparison operator, and value to use in the search.

14 Examples Dim strName As String strName = "John Smith"
rstCurrent.Find "fldID = 123" rstCurrent.Find "fldName = 'John Smith'" rstCurrent.Find "fldName = '" & strName & "'"

15 Private Sub cboMemberID_Click() Dim strTemp As String
strTemp = cboMemberID.Text rstCurrent.MoveFirst rstCurrent.Find "[Member ID] = '" & strTemp & "'" Call LoadCurrentRecord End Sub In this example, LoadCurrentRecord is a general sub procedure that fills the form text fields with information from the recordset.


Download ppt "Programming the RecordSet Object"

Similar presentations


Ads by Google