Forwardonly

The forward-only cursor (DAO equivalent: dbOpenForwardOnly) allows only forward movement through a recordset and doesn't show additions, changes, or deletions made by other users.

It is the default cursor type. For the fastest access to data that you don't need to modify, use a forward-only cursor and the adLockReadOnly lock type, as in the TestForwardReadOnly procedure that follows; if you do need to modify the data, use the adLockOptimistic lock type instead:

Private Sub TestForwardReadOnly()

On Error GoTo ErrorHandler

Dim cnn As ADODB.Connection Dim rst As ADODB.Recordset

Create a connection to the current database.

Set cnn = CurrentProject.Connection Set rst = New ADODB.Recordset

Create a recordset based on a select query.

rst.Open Source:="qryCompanyAddresses", _ ActiveConnection:=cnn.ConnectionString, _ CursorType:=adOpenForwardOnly, _ LockType:=adLockReadOnly

Iterate through the query, and print values from its fields to the Immediate window.

Do While Not rst.EOF

Debug.Print "Company ID: " & rst![CompanyID] _ & vbCrLf & vbTab & "Category: " _ & rst![Category] _

& vbCrLf & vbTab & "Company Name: " _ & rst![Company] & vbCrLf rst.MoveNext Loop

ErrorHandlerExit:

Close the Recordset and Connection objects.

If Not rst Is Nothing Then

If rst.State = adStateOpen Then rst.Close Set rst = Nothing End If End If

If Not cnn Is Nothing Then

If cnn.State = adStateOpen Then cnn.Close

Set cnn = Nothing End If End If

Exit Sub

ErrorHandler:

& "; Description: " & Err.Description Resume ErrorHandlerExit

End Sub

Data from each record is printed to the Immediate window; the last two records' data is listed here:

Company ID: Yclept Yarbro Category: Books Company Name: Yclept Yarbro

Company ID: ZDExpos Category: Computer Company Name: ZDExpos

0 0

Post a comment