Results 1 to 4 of 4
  1. #1
    Kivan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Posts
    27

    Deleting current record using VBA

    Hi,

    I would like to create a delete button in my form. But before the record will be deleted I want to force user to confirm that he really want to delete it. So obviously I need a checkbox, but I dont know how to write delete function in it. I know it should be like this.



    Code:
    Private Sub DeleteButton_Click()
    If MsgBox(Prompt:="My question", Buttons:=vbYesNo) = vbYes Then
     
    Else
    
    End If
    End Sub
    Yes I know it is very easy problem and maybe that's why I couldnt have found a solution for it and I dont know anything about VBA.
    Thanks for your time .

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Try:

    Private Sub DeleteButton_Click()
    If MsgBox(Prompt:="My question", Buttons:=vbYesNo) = vbYes Then
    CurrentDb.Execute "DELETE FROM tablename WHERE ID=" & Me.ID
    Else
    MsgBox "Delete canceled"
    End If
    End Sub

    Probably want to deal with keyboard delete key so users can't delete record that way. Use form BeforeDelConfirm event to intercept that key input. http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Kivan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Posts
    27
    Thank you for your reply.

    No I dont want to deal with keyboard button. The button I deal with is placed on a form. Your soultion looks good but I'm getting error and I have no idea how to solve it. The error:

    Code:
    Run-time error '3061'.
    Too less parameters. Expected 1.
    And the code.

    Code:
    Private Sub Delete_Click()
    If MsgBox(Prompt:="My question", Buttons:=vbYesNo) = vbYes Then
    CurrentDb.Execute "DELETE FROM Users WHERE ID=" & Me.IdUser
    Else
    MsgBox "Delete canceled"
    End If
    End Sub
    Any idea?

    Edit: Ah ok I solved it already. Forgot to change where Id to IdUser.

    Thanks a lot . Problem Solved.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Do you want user able to delete with the keyboard Delete key? If you don't intercept that key input they can delete record without the verification unless maybe you have the form property AllowDeletion set to No.

    No need for checkbox unless you want to allow selection of multiple records for deletion. In which case the checkbox would have to bound to a yes/no field in table.

    BTW, this syntax works for the MsgBox: MsgBox("My question", vbYesNo+vbQuestion) = vbYes
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Deleting record and related records
    By TedT in forum Access
    Replies: 2
    Last Post: 10-25-2011, 04:26 PM
  2. Deleting a record
    By alexc333 in forum Access
    Replies: 7
    Last Post: 07-26-2011, 10:43 AM
  3. Problem deleting record
    By Vikki in forum Access
    Replies: 8
    Last Post: 02-26-2010, 11:07 AM
  4. Error when deleting a record
    By access in forum Forms
    Replies: 5
    Last Post: 06-15-2009, 12:33 PM
  5. Deleting Record Contents, not complete record...
    By Charles Waters in forum Access
    Replies: 2
    Last Post: 06-24-2008, 12:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums