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?
Bookmarks