Private Sub DeleteRecord_Click()
Dim Reply As Variant
Reply = MsgBox("THIS ACTION DELETES THIS RECORD." & Chr(10) & Chr(10) & "DO YOU WANT TO DO THIS?", vbYesNo)
If Reply = 7 Then 'NO
DoCmd.CancelEvent
Exit Sub
End If
If Reply = 6 Then 'Yes
DoCmd.RunCommand acCmdDeleteRecord
Me.Requery
End If
End Sub
Private Sub DeleteRecord_Click()
Dim rsp As VbMsgBoxResult
rsp = MsgBox( _
"THIS ACTION DELETES THIS RECORD." & vbCrLf & "DO YOU WANT TO DO THIS?", _
vbYesNo + vbExclamation + vbDefaultButton2, _
"Confirm Delete")
If rsp = vbYes Then
DoCmd.RunCommand acCmdDeleteRecord
Me.Requery
End If
End Sub
Code does not workJust to show some other options...
• The MsgBox() function returns a member of the vbMsgBoxResult enumeration, which can make the code a little clearer.Code:Private Sub DeleteRecord_Click() Dim rsp As VbMsgBoxResult rsp = MsgBox( _ "THIS ACTION DELETES THIS RECORD." & vbCrLf & "DO YOU WANT TO DO THIS?", _ vbYesNo + vbExclamation + vbDefaultButton2, _ "Confirm Delete") If rsp = vbYes Then DoCmd.RunCommand acCmdDeleteRecord Me.Requery End If End Sub
• For Deletes, I like to set the default button to <No> using vbDefaultButton2, so the user has to to explicitly click <Yes>.
• You don't have to handle the vbNo (7) case. If it's not vbYes, the delete will not occur, and nothing changes.
: )
Delete does not work on the Authors Entry screen, the delete buttonCode does not work
View attachment 116720
Yes I want to delete the selected item in the combo boxI am not sure what a combo box has to do with anything, but I am guessing you have not described your issue correctly.
Are you expecting to delete that entry in the combo?
I was just about to ask that!!Yes I want to delete the selected item in the combo box
And is the combo recordsource a query?
Private Sub Delete_But_Click()
Dim strSQL As String
If MsgBox("Are you sure you want to delete this author?", vbYesNo) = vbYes Then
strSQL = "DELETE * FROM [Authors TBL] WHERE AuthorsID = " & AuhorsNameCBO
DoCmd.RunSQL (strSQL)
Me.AuhorsNameCBO.Requery
End If
End Sub
The relationship between authors and books has been set, so the delete will not happen if there are books by the author.It depends on whether you want to remove an author even if there are related books. If you do want that, the books must also be removed. To achieve this, you must adjust a property of the relationship between authors and books (enable cascade delete). You can then delete an author and the associated books with the following code:
If an author for whom books are present in the database cannot be removed, then it is a different story. Then please let us know.Code:Private Sub Delete_But_Click() Dim strSQL As String If MsgBox("Are you sure you want to delete this author?", vbYesNo) = vbYes Then strSQL = "DELETE * FROM [Authors TBL] WHERE AuthorsID = " & AuhorsNameCBO DoCmd.RunSQL (strSQL) Me.AuhorsNameCBO.Requery End If End Sub
Received error message "Invalid Outside Procedure"Just to show some other options...
• The MsgBox() function returns a member of the vbMsgBoxResult enumeration, which can make the code a little clearer.Code:Private Sub DeleteRecord_Click() Dim rsp As VbMsgBoxResult rsp = MsgBox( _ "THIS ACTION DELETES THIS RECORD." & vbCrLf & "DO YOU WANT TO DO THIS?", _ vbYesNo + vbExclamation + vbDefaultButton2, _ "Confirm Delete") If rsp = vbYes Then DoCmd.RunCommand acCmdDeleteRecord Me.Requery End If End Sub
• For Deletes, I like to set the default button to <No> using vbDefaultButton2, so the user has to to explicitly click <Yes>.
• You don't have to handle the vbNo (7) case. If it's not vbYes, the delete will not occur, and nothing changes.
: )
Private Sub Delete_But_Click()
Dim Reply As Variant
Dim strSQL As String
Reply = MsgBox("THIS ACTION DELETES THIS RECORD." & Chr(10) & Chr(10) & "DO YOU WANT TO DO THIS?", vbYesNo)
If Reply = 7 Then 'NO
DoCmd.CancelEvent
Exit Sub
End If
If Reply = 6 Then 'Yes
'DoCmd.OpenQuery "DeleteAuthor", acViewNormal, acReadOnly
strSQL = "DELETE [Authors TBL].AuthorsID, [Authors TBL].AuthorsName FROM [Authors TBL]"
strSQL = strSQL & " WHERE [Authors TBL].AuthorsID = " & Me.AuhorsNameCBO
Debug.Print strSQL
CurrentDb.Execute strSQL
Me.Requery
Me.Recalc
End If
End Sub
I would probably not use a combo box either. I would use a search textbox to find the record and then use a delete query with the selected record ID as the criteria. But I like to actually view the record I am deleting, but that's just me. Using a combo box is a little dangerous, especially if you have multiple users.I would probably use the Execute, so as not to get the warnings.
One way.
Code:Private Sub Delete_But_Click() Dim Reply As Variant Dim strSQL As String Reply = MsgBox("THIS ACTION DELETES THIS RECORD." & Chr(10) & Chr(10) & "DO YOU WANT TO DO THIS?", vbYesNo) If Reply = 7 Then 'NO DoCmd.CancelEvent Exit Sub End If If Reply = 6 Then 'Yes 'DoCmd.OpenQuery "DeleteAuthor", acViewNormal, acReadOnly strSQL = "DELETE [Authors TBL].AuthorsID, [Authors TBL].AuthorsName FROM [Authors TBL]" strSQL = strSQL & " WHERE [Authors TBL].AuthorsID = " & Me.AuhorsNameCBO Debug.Print strSQL CurrentDb.Execute strSQL Me.Requery Me.Recalc End If End Sub