PRB: Use Open Method to Change CursorType and LockType

ID: Q188857


The information in this article applies to:


SYMPTOMS

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.


CAUSE

When execute is used to open a recordset, it is creating a recordset with default properties.


RESOLUTION

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.


STATUS

This behavior is by design.


MORE INFORMATION

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.

Steps to Reproduce Behavior

  1. Create a Standard EXE project and select Microsoft Active Data Objects Library 1.5 (on the Project menu, click References).


  2. Place seven command buttons on Form1.


  3. Paste the following code in the Form1 code window:
    
       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 


  4. Run the code, and click Connect. Make sure the Immediate Window is open; if it is not open, select it from the View menu.


  5. Click connection.execute; then select the print recordset attributes and close recordset. Note the print out is in the immediate window.


  6. Click command.execute, followed by selecting print recordset attributes and close recordset. Note the print out is in the Immediate Window.


  7. Click the open recordset followed by selecting print recordset attributes and the close recordset. Again, note the print out in the Immediate Window.


You may test the above sequences for the Connection, Command, and Open methods in any order, and the corresponding print outs will not change for Cursorlocation, Cursortype, and Locktype.

The following table lists the meanings of these numbers:


        Cursorlocation    Cursortype     Locktype

   ----------------------------------------------

   0    N/A              adforwardonly   N/A
   1    N/A              adopenkeyset    readonly
   2    aduseserver      adopendynamic   pessimistic
   3    aduseclient      adopenstatic    optimistic 


REFERENCES

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