PRB: Use Open Method to Change CursorType and LockTypeID: Q188857
|
If a recordset is opened through a connection object or a command object, the CursorType defaults to adOpenForwardOnly, and the LockType defaults to adLockReadOnly if the cursorlocation is adUseServer. Changing these properties before creating the recordset does not cause an error, but the properties are not preserved.
When execute is used to open a recordset, it is creating a recordset with default properties.
Use the open method to create the recordset. The preceding properties of the recordset may then be set before the open statement or they may be set with the open statement.
This behavior is by design.
The default CursorLocation is adUseServer, and the default LockType is adLockReadOnly. The default CursorType for adUseServer is
adOpenForwardOnly.
The CursorLocation can be set on the connection prior to opening the
recordset. This CursorLocation is applied to the recordset opened on this
connection. If the connection's CursorLocation is set to adUseClient, the
resulting recordset CursorLocation would be adUseClient. The CursorType for
adUseClient is always adOpenStatic. The default LockType remains
adLockReadOnly.
Option Explicit
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Private Sub Command1_Click()
With cn
.ConnectionString = "dsn=yourdatasourcename"
.Open
End With
cn.CursorLocation = adUseServer 'Set on connection here.
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
End Sub
Private Sub Command2_Click()
Set rs = cn.Execute("select * from authors")
End Sub
Private Sub Command3_Click()
With cmd
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = "select * from authors"
End With
Set rs = cmd.Execute
End Sub
Private Sub Command4_Click()
rs.Open "select * from authors", cn
End Sub
Private Sub Command5_Click()
Debug.Print "CursorLocation " & rs.CursorLocation; ""
Debug.Print "cursortype " & rs.CursorType
Debug.Print "locktype " & rs.LockType
End Sub
Private Sub Command6_Click()
If rs.State = 1 Then
rs.Close
End If
If Not rs Is Nothing Then
Set rs = Nothing
End If
rs.CursorLocation = adUseServer
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
End Sub
Private Sub Command7_Click()
If Not rs Is Nothing Then
Set rs = Nothing
End If
If cn.State = 1 Then
cn.Close
End If
If Not cmd Is Nothing Then
Set cmd = Nothing
End If
If Not cn Is Nothing Then
Set cn = Nothing
End If
Unload Me
End Sub
Private Sub Form_Load()
Command1.Caption = "connect"
Command2.Caption = "connection.execute"
Command3.Caption = "command.execute"
Command4.Caption = "open recordset"
Command5.Caption = "print recordset attributes"
Command6.Caption = "close recordset"
Command7.Caption = "quit"
End Sub
Cursorlocation Cursortype Locktype
----------------------------------------------
0 N/A adforwardonly N/A
1 N/A adopenkeyset readonly
2 aduseserver adopendynamic pessimistic
3 aduseclient adopenstatic optimistic
Microsoft Developer Network CD, search on: "locktype"; "ADO Data Control Constants."
Additional query words: kbado
Keywords : kbADO
Version : WINDOWS:1.5,2.0
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: May 27, 1999