Access.2007.VBA.Bibl..

(John Hannent) #1
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 adLockReadOnlylock type, as in the TestForwardReadOnly
procedure that follows; if you do need to modify the data, use the adLockOptimisticlock
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

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf