ADO 2.7 Samples

Save and Open Methods Example (VB)

These three examples demonstrate how the Save and Open methods can be used together.

Assume you are going on a business trip and want to take along a table from a database. Before you go, you access the data as a Recordset and save it in a transportable form. When you arrive at your destination, you access the Recordset as a local, disconnected Recordset. You make changes to the Recordset, then save it again. Finally, when you return home, you connect to the database again and update it with the changes you made on the road.

First, access and save the Authors table.

'BeginSaveVB

    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string
    
Public Sub SaveX1()

    'recordset and connection variables
   Dim rstAuthors As ADODB.Recordset
   Dim Cnxn As ADODB.Connection
   Dim strCnxn As String
   Dim strSQLAuthors As String
    
   ' Open connection
   Set Cnxn = New ADODB.Connection
   strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; "
   Cnxn.Open strCnxn

   Set rstAuthors = New ADODB.Recordset
   strSQLAuthors = "SELECT au_id, au_lname, au_fname, city, phone FROM Authors"
   rstAuthors.Open strSQLAuthors, Cnxn, adOpenDynamic, adLockOptimistic, adCmdText
    
   'For sake of illustration, save the Recordset to a diskette in XML format
   rstAuthors.Save "a:\Pubs.xml", adPersistXML
    
    'clean up
   rstAuthors.Close
   Cnxn.Close
   Set rstAuthors = Nothing
   Set Cnxn = Nothing
    
End Sub
'EndSaveVB

At this point, you have arrived at your destination. You will access the Authors table as a local, disconnected Recordset. Don't forget you must have the MSPersist provider on the machine that you are using in order to access the saved file, a:\Pubs.xml.

'BeginSave2VB
Public Sub SaveX2()

    Dim rst As ADODB.Recordset
    Set rst = New ADODB.Recordset
    
    'For sake of illustration, we specify all parameters
    rst.Open "a:\Pubs.xml", "Provider=MSPersist;", adOpenForwardOnly, adLockBatchOptimistic, adCmdFile
    
    'Now you have a local, disconnected Recordset - Edit as you desired
    '(In this example the change makes no difference)
    rst.Find "au_lname = 'Carson'"
    If rst.EOF Then
       Debug.Print "Name not found."
       Exit Sub
    End If
    
    rst!city = "Chicago"
    rst.Update
    
    'Save changes in ADTG format this time, purely for sake of illustration.
    'Note that the previous version is still on the diskette, as a:\Pubs.xml.
    rst.Save "a:\Pubs.adtg", adPersistADTG
    rst.Close
    Set rst = Nothing
    
End Sub
'EndSave2VB

Finally, you return home. Now update the database with your changes.

'BeginSave3VB
Public Sub SaveX3()
    
    'To integrate this code
    'replace the data source and initial catalog values
    'in the connection string
    
    Dim Cnxn As New ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim strCnxn As String
    
    Set rst = New ADODB.Recordset
    ' The lock mode is batch optimistic because we are going to
    ' use the UpdateBatch method.
    rst.Open "a:\Pubs.adtg", "Provider=MSPersist;", adOpenForwardOnly, adLockBatchOptimistic, adCmdFile
    
     ' Connect to the database, associate the Recordset with the connection
     ' then update the database table with the changed Recordset
    strCnxn = "Provider=SQLOLEDB;Data Source=MyServer;User Id=sa;Password=pwd;Initial Catalog=pubs;"
    Cnxn.Open strCnxn
    
    rst.ActiveConnection = Cnxn
    rst.UpdateBatch
    
     ' clean up
    rst.Close
    Cnxn.Close
    Set rst = Nothing
    Set Cnxn = Nothing
    
End Sub
'EndSave3VB

See Also

Open Method (ADO Recordset) | Recordset Object | Recordset Persistence | Save Method

© 1998-2001 Microsoft Corporation. All rights reserved.