meilkew
New member
- Local time
- Today, 23:00
- Joined
- Apr 14, 2020
- Messages
- 27
Hello Guys,
I have a List Box in UserForm and Command Button to Delete existing records in Access Database. The below code works for deleting multiple records selected in the list box. I would like to grab a copy of these records into a new Table "f_SD_DeletedRecords" before executing the command. Additionally, I would like to add new fields for "Date_Deleted", "Deleted_By" and "Reason" who execute and why the records are being deleted. The table where the records are being deleted have the same number of columns of f_SD_DeletedRecords + Date Deleted + Deleted By + Reason.
I hope someone could help me modify this code. If required, I can share the template, that I am currently working on.
Files that I am currently working.
I have a List Box in UserForm and Command Button to Delete existing records in Access Database. The below code works for deleting multiple records selected in the list box. I would like to grab a copy of these records into a new Table "f_SD_DeletedRecords" before executing the command. Additionally, I would like to add new fields for "Date_Deleted", "Deleted_By" and "Reason" who execute and why the records are being deleted. The table where the records are being deleted have the same number of columns of f_SD_DeletedRecords + Date Deleted + Deleted By + Reason.
I hope someone could help me modify this code. If required, I can share the template, that I am currently working on.
Code:
Private Sub cmbDelete_Click()
Dim sFilePath As String
sFilePath = Worksheets("Home").Range("P4").Value
If Selected_List = 0 Then
MsgBox "Please select record(s) to delete.", vbOKOnly + vbInformation, "Delete"
Exit Sub
End If
Dim confirmation As Integer
confirmation = MsgBox("Do you want to delete selected record(s)?", vbQuestion + vbYesNo)
If confirmation = vbNo Then Exit Sub
Dim i As Long
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim qry As String
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sFilePath
For i = 0 To Me.ListBoxSD.ListCount - 1
If Me.ListBoxSD.Selected(i) = True Then
qry = "Delete FROM f_SD WHERE ID = '" & Me.ListBoxSD.List(i, 0) & "'"
rst.Open qry, cnn, adOpenKeyset, adLockOptimistic
End If
Next i
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Call Me.List_box_Data
MsgBox "Deleted Successfully", vbInformation
End Sub
Files that I am currently working.
Attachments
Last edited: