+ Reply to Thread
Results 1 to 2 of 2

Cut paste recordset from one table to another (ADODB)

Hybrid View

  1. #1
    Registered User
    Join Date
    03-09-2016
    Location
    Rotterdam
    MS-Off Ver
    2016
    Posts
    28

    Cut paste recordset from one table to another (ADODB)

    Dear all,

    I would like to know how I can cut and paste one ADODB recordset from a table to another. A recordset is just one record that is specified by the user. So for example: the user identifies record ID 60 as wrong and wants to delete it. The code should cut the record from Access_Database and paste it into Access_Database2.
    I've got the following code so far:
    Private Sub cmdDelete_db_Click()
    
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim dbPath As String
    Dim i As Integer
    Dim x As Integer
    
    On Error GoTo errHandler:
        If Me.Delete0.Value = "" Then
            MsgBox "Unknown ID number. Unable to delete without ID", _
            vbOKOnly Or vbInformation, "Insufficent data"
            Exit Sub
        End If
        
        If MsgBox("Deleting a record can not be reversed" _
        & vbCrLf & "do you want to proceed?", vbYesNo + vbCritical, "Confirm action") = vbNo Then Exit Sub
        
    dbPath = "A:\Desktop\XXXXXXXXXX.accdb"
    
    Set cnn = New ADODB.Connection
    
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
    
    Set rs = New ADODB.Recordset
        
    rs.Open "SELECT * FROM Access_Database " & _
        "WHERE ID = " & CLng(Me.Delete0), ActiveConnection:=cnn, _
        CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
        Options:=adCmdText
    
        If rs.EOF And rs.BOF Then
            rs.Close
            cnn.Close
            Set rs = Nothing
            Set cnn = Nothing
            Application.ScreenUpdating = True
            MsgBox "The record isn't found. Process canceled.", vbCritical, "No Records"
            Exit Sub
        End If
    'Before deleting the record, it should be copied from one table to table: Access_database2. In that way, im sure there is always a duplicate of the deleted record. 
    rs.Delete
    
    For x = 0 To 14
        Me.Controls("Delete" & x).Value = ""
    Next
    
    rs.Close
    cnn.Close
    Set rs = Nothing
    Set cnn = Nothing
    
    Application.ScreenUpdating = True
    ImportFromAccess
    Me.lstDataFromAccess.RowSource = "DataAccess"
    MsgBox "Data has been deleted", vbInformation, "Deletion successful"
    
    On Error GoTo 0
    Exit Sub
    errHandler:
    'clear memory
    Set rs = Nothing
    Set cnn = Nothing
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdDelete"
    
    End Sub
    Can someone help me with this?

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Cut paste recordset from one table to another (ADODB)

    Have a look at the 'INSERT INTO' SQL statement to add the record to the 2nd db, and the DELETE statement to remove it from the first...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] ADODB recordset on excel table return null value when it shouldn't
    By lripoll in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-13-2016, 03:28 AM
  2. Change ADODB.Recordset by New
    By Remphan in forum Access Tables & Databases
    Replies: 1
    Last Post: 03-13-2016, 07:51 PM
  3. Refresh ADODB Recordset
    By oteixeira in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-01-2011, 04:12 AM
  4. Check if equal: Cells with adodb recordset
    By Huija in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-09-2008, 01:25 AM
  5. [SOLVED] ADODB.Recordset from workbooks : numbers only?
    By JVLin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2006, 12:00 AM
  6. How do I retrieve the column names in a ADODB recordset from MS SQ
    By MChrist in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2005, 09:05 AM
  7. Delete ADODB Recordset
    By Jim Thomlinson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-12-2005, 01:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1