Results 1 to 8 of 8
  1. #1
    enquiries is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2013
    Location
    Samoa
    Posts
    69

    Post Running SQL Del statement in VBA which works but have to get Delete response first

    Hi AccessForum users
    I'm running a sql statement code below in VBA and it works perfectly fine and deletes the record from the table but:
    1. It only runs when I select yes after being prompted if I'm sure to delete the record. I need to do an if statement that takes the response of the button selected (yes or no) and if Yes run the sql statement otherwise cancel the action because if I select No when prompted it will give an error.



    Dim DelIdRcd As Integer
    DelIdRcd = Me.DirectorshipExperienceID.Value
    Dim QryDel As String
    QryDel = "Delete * From BoardMemberPositions where DirectorshipExperienceID = " & DelIdRcd
    DoCmd.RunSQL (QryDel)

    2. After it deletes the record, The word #DELETE appears in all the fields, although I can create a new record and than save and close the form down and open it again and the #DELETE record will be gone..... so logically it work but it's untidy and I would prefer to have all the cells cleared after deleting

    I tried doing:

    If Me.AfterDelConfirm Then
    DelIdRcd = Me.DirectorshipExperienceID.Value
    Dim QryDel As String
    QryDel = "Delete * From BoardMemberPositions where DirectorshipExperienceID = " & DelIdRcd
    DoCmd.RunSQL (QryDel)
    End If

    But that doesn't work

    Thanks in advance for any help - much appreciated

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    The prompt you are getting is not associated with your VBA. Therefore, you get an error when you click no. You can suppress the prompt by using something like
    DoCmd.SetWarnings False
    DoCmd.RunSQL (QryDel)
    DoCmd.SetWarnings True

    The preferred method is not to use Docmd.RunSQL
    CurrentDb.Execute QryDel

    Either method will avoid the ability to abort. If you want to include the ability for the user to exit VBA without executing, use a msgbox with an argument that includes vbYesNo or vbOKCancel.

    Take a look at the help files for MsgBox and post back with any questions for us.

  3. #3
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    As far as your 2nd question, you should Requery the form or control after running this code.

  4. #4
    enquiries is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2013
    Location
    Samoa
    Posts
    69
    Thank you both for your help - am getting somehwere
    1. ItsMe - Okay, I've managed to get it working with:
    If MsgBox("Are you sure you want to delete this record?", vbQuestion + vbYesNo) = vbYes Then
    CurrentDb.Execute QryDel
    Me.Requery
    End If

    But I still need to click somewhere else on the form before click on the DELETE Button, it won't let me delete if I've just entered in the details - I'm not sure if this will be a problem, but I prefer it would.

    2. ipisors - I added on Form.Requery and that clears it up perfectly thanks



    Thanks again very much for your help

  5. #5
    enquiries is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2013
    Location
    Samoa
    Posts
    69
    I can't figure out how to delete my posts Lol - so I've just removed the wording from this and reedit it as a side question. How do you delete posts on here please? Thanks

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by enquiries View Post
    ...ill need to click somewhere else on the form before click on the DELETE Button, it won't let me delete if I've just entered in the details...
    I am not sure I follow. You can save the current record before running your query to ensure newly entered data is included in the SQL action query.

    If me.dirty = true then me.dirty = false

  7. #7
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    >>But I still need to click somewhere else on the form before click on the DELETE Button, it won't let me delete if I've just entered in the details - I'm not sure if this will be a problem, but I prefer it would

    I think I agree with ItsMe basically but I am just curious on the details.....By 'won't let me delete' what exactly happens? Visually nothing? An Error?

  8. #8
    enquiries is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2013
    Location
    Samoa
    Posts
    69
    Got it fixed - just added an UNDO button that deletes the record if it hasn't been saved and the DELETE button works perfectly as it is.

    Thanks for all your help

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

Similar Threads

  1. Replies: 2
    Last Post: 01-15-2014, 03:50 PM
  2. Running a Select statement in VBA
    By geraldk in forum Forms
    Replies: 12
    Last Post: 05-04-2012, 01:23 PM
  3. Replies: 7
    Last Post: 03-15-2011, 11:14 PM
  4. running select query in form delete event
    By suki360 in forum Programming
    Replies: 0
    Last Post: 03-11-2011, 10:11 AM
  5. Running a Delete (Records) Query on Close
    By NoiCe in forum Queries
    Replies: 1
    Last Post: 07-12-2009, 06:17 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