Share via


Editing Data

We've explained how use ADO to connect to a data source, execute a command, get the results in a Recordset object, and navigate within the Recordset. This section focuses on the next fundamental ADO operation: editing data.

This section continues to use the sample Recordset introduced in Examining Data, with one important change. The following code is used to open the Recordset:

    . . .
Attribute VB_Name = "modEditingData"


    . . .

The important change to the code involves setting the Connection object's CursorLocation property equal to adUseClient in the GetNewConnection function (shown below), which indicates the use of a client cursor. For more information about the differences between client-side and server-side cursors, see Understanding Cursors and Locks.

The CursorLocation property's adUseClient setting moves the location of the cursor from the data source (the SQL Server, in this case) to the location of the client code (the desktop workstation). This setting forces ADO to invoke the Client Cursor Engine for OLE DB on the client in order to create and manage the cursor.

You might also have noticed that the LockType parameter of the Open method changed to adLockBatchOptimistic. This opens the cursor in batch mode. (The provider caches multiple changes and writes them to the underlying data source only when you call the UpdateBatch method.) Changes made to the Recordset will not be updated in the database until the UpdateBatch method is called.

Finally, the code in this chapter uses a modified version of the GetNewConnection function, introduced in Chapter 2. This version of the function now returns a client-side cursor. The function looks like this:

'BeginNewConnection
Public Function GetNewConnection() As ADODB.Connection
    On Error GoTo ErrHandler:
    
    Dim objConn As New ADODB.Connection
    Dim strConnStr As String
    
    strConnStr = "Provider='SQLOLEDB';Initial Catalog='Northwind';" & _
                 "Data Source='MySqlServer';Integrated Security='SSPI';"
             
    objConn.ConnectionString = strConnStr
    objConn.CursorLocation = adUseClient
    objConn.Open
    
    Set GetNewConnection = objConn
    
    Exit Function
    
ErrHandler:
    Set objConn = Nothing
    Set GetNewConnection = Nothing
    
    If Err <> 0 Then
        MsgBox Err.Source & "-->" & Err.Description, , "Error"
    End If
End Function
'EndNewConnection

This section contains the following topics.