ADO 2.7 Samples

DeleteRecord and MoveRecord Methods Example (VB)

This example demonstrates how to copy, move, edit, and delete the contents of a text file published to a Web folder. Other properties and methods used include GetChildren, ParentURL, Source, and Flush.

'BeginDeleteRecordVB

    'to use this code replace all connection
    'and file/folder variables
    
Public Sub DeleteRecordX()

     ' connection and recordset variables
    Dim rsDestFolder As ADODB.Recordset
    Dim Cnxn As ADODB.Connection
    Dim strCnxn As String
 
     ' file as record variables
    Dim rcFile As ADODB.Record
    Dim rcDestFile As ADODB.Record
    Dim rcDestFolder As ADODB.Record
    Dim objStream As Stream
    
     ' file variables
    Dim strFile As String
    Dim strDestFile As String
    Dim strDestFolder As String
            
     ' instantiate variables
    Set rsDestFolder = New ADODB.Recordset
    Set rcDestFolder = New ADODB.Record
    Set rcFile = New ADODB.Record
    Set rcDestFile = New ADODB.Record
    Set objStream = New ADODB.Stream
     
     ' open a record on a text file
    Set Cnxn = New ADODB.Connection
    strCnxn = "url=http://a-dgayne2/"
    Cnxn.Open strCnxn
    strFile = "test/test2.txt"
    rcFile.Open strFile, Cnxn, adModeReadWrite, adOpenIfExists Or adCreateNonCollection
        
     ' edit the contents of the text file
    objStream.Open rcFile, , adOpenStreamFromRecord
    
    Debug.Print "Source: " & strCnxn & rcFile.Source
    Debug.Print "Original text: " & objStream.ReadText
    
    objStream.Position = 0
    objStream.WriteText "Newer Text"
    objStream.Position = 0
    
    Debug.Print "New text: " & objStream.ReadText
    
     ' reset the stream object
    objStream.Flush
    objStream.Close
    rcFile.Close
    
     ' reopen record to see new contents of text file
    rcFile.Open strFile, Cnxn, adModeReadWrite, adOpenIfExists Or adCreateNonCollection
    objStream.Open rcFile, adModeReadWrite, adOpenStreamFromRecord
    
    Debug.Print "Source: " & rcFile.Source
    Debug.Print "Edited text: " & objStream.ReadText
    
     ' copy the file to another folder
    strDestFile = "test2/test.txt"
    rcFile.CopyRecord strFile, strDestFile, , , adCopyOverWrite
    
     ' delete the original file
    rcFile.DeleteRecord
    
     ' move the file from the subfolder back to original location
    rcDestFolder.Open strDestFolder, Cnxn, adOpenIfExists Or adCreateCollection
    Set rsDestFolder = rcDestFolder.GetChildren
    rsDestFolder.MoveFirst
    
     ' position current record at on the correct file
    Do While Not rsDestFolder.EOF Or rsDestFolder(0) = "test1.txt"
        rsDestFolder.MoveNext
    Loop
    
     ' open a record on the correct row of the recordset
    rcDestFile.Open rsDestFolder, Cnxn
    
     ' do the move
    rcDestFile.MoveRecord strDestFile, strFile, , , adMoveOverWrite
    
End Sub
'EndDeleteRecordVB

See Also

DeleteRecord Method | Flush Method | GetChildren Method | MoveRecord Method | ParentURL Property | Source Property (ADO Record)

© 1998-2001 Microsoft Corporation. All rights reserved.