Alaa Eldin
Alaa Eldin

Reputation: 13

Access VBA code that delete records not working

hi guys i tried that code on access vba and it giving me error here is it :

Private Sub Command102_Click()
If msgbox("are u sure", MsgBoxStyle.yesno, "Delete") = MsgBoxResult.Yes Then
 Resume
    msgbox ("deleted")
    Else
    msgbox ("canceld")
End If
    DoCmd.RunCommand acCmdDeleteRecord
End Sub

Upvotes: 1

Views: 12309

Answers (1)

Ioannis
Ioannis

Reputation: 5388

VBA does not understand this code because it is written for VB.NET. If it is the first time you hear about VB.NET, think of it as an extension of VBA (this is a huge oversimplification and I hope I dont get downvoted because of writing such stuff :) ).

In VBA syntax you would do something like:

Private Sub Command102_Click()
    If MsgBox(Prompt:="Are you sure?", Buttons:=vbYesNo, Title:="Delete") = vbYes Then
         On Error Resume Next
         DoCmd.RunCommand acCmdDeleteRecord
         If Err.Number = 0 Then
            MsgBox Prompt:="Deleted", Buttons:=vbOKOnly, Title:="Deleted"
        Else
            MsgBox Prompt:="There is no record to delete!", Buttons:=vbOKOnly, Title:="Error"
        End If
    Else
        MsgBox Prompt:="Canceled", Buttons:=vbOKOnly, Title:="Canceled"
    End If
End Sub

You do not need Resume in this context.

Have a look at this post as well, it is quite similar.

Hope this helps!

Upvotes: 2

Related Questions