Results 1 to 10 of 10
  1. #1
    soco3594 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2015
    Location
    Australia
    Posts
    20

    Continuous Form - Delete Selected Records

    Hi guys,

    I thought this would be easier to find but haven't had any luck. I have a continuous form based on a simple table. One of the fields in the table is called "Select" with Yes/No data type, which allows the user to select 1 or more records on the form.

    I want to add a "Delete" button to the form using which deletes only those records which are selected via VBA (i.e. "Select" checkbox ticked). So the button will find the records for which Select = True, prompt the user for confirmation, then delete those records.

    Cheers

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,459
    code behind the button click event would be something like

    Code:
    if msgbox("OK to Delete?",vbyesnocancel")=vbyes then
        currentdb.execute("DELETE * FROM myTable WHERE Selected=True")
    end if
    note that Select (which you have called your checkbox) is a reserved word, so change it to something else (e.g. selected)

  3. #3
    soco3594 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2015
    Location
    Australia
    Posts
    20
    The code doesn't work every time for some reason... If I select a record and click delete, nothing happens. If I exit and re-enter the form and click delete (with the "selected" field still checked) the record deletes.

    Should I be adding a refresh or requery command in there somewhere?

  4. #4
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    try adding a
    Code:
    me.requery
    to your code and see if that fixes your issue.

  5. #5
    soco3594 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2015
    Location
    Australia
    Posts
    20
    I ended up adding a delete statement to the "close" button which actually achieves what I was trying to achieve more efficiently. Basically I wanted blank records to be deleted when I close the form (i.e. records which have been assigned an auto-number but for which no details were entered).

    Similarly, if you delete the description against a record prior to closing the form, that record will also be deleted:

    Private Sub btnClose_Click()

    Dim sql As String
    sql = "DELETE * FROM tblMilestonesTasks WHERE MilestoneTaskDescription Is Null"
    CurrentDb.Execute sql

    DoCmd.Save
    DoCmd.Close

    End Sub

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Just an FYI.....

    DoCmd.Save
    This line is not needed. It is for saving changes to an object, not the data in a form.

    Closing the form automatically saves changes to data. If you want to explicitly save data, use
    Code:
    Me.Dirty = FALSE
    or
    Code:
    If Me.Dirty Then
       Me.Dirty = FALSE
    End If

  7. #7
    soco3594 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2015
    Location
    Australia
    Posts
    20
    Thanks. I actually had that in there from a previous iteration of the code, but you're right it's of no use in the current context.

    Cheers

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by soco3594 View Post

    ...If I select a record and click delete, nothing happens. If I exit and re-enter the form and click delete (with the "selected" field still checked) the record deletes...
    If your 'delete' button is present on each Record, the problem is likely that the change to the Record, i.e. ticking the Checkbox, hasn't been saved, yet. When you exit the Form and return, it has been saved, and hence works!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    soco3594 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2015
    Location
    Australia
    Posts
    20
    Of course! Perhaps adding a save command before the delete command would do the trick?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,973
    Use ssanfu's suggestion or:

    DoCmd.RunCommand acCmdSaveRecord

    The Requery should accomplish the same. Except Requery resets focus to the first record.
    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. Replies: 3
    Last Post: 12-28-2015, 04:11 PM
  2. Replies: 8
    Last Post: 11-28-2015, 12:00 PM
  3. Continuous search form won't open selected record in view form.
    By IncidentalProgrammer in forum Programming
    Replies: 20
    Last Post: 03-24-2015, 02:53 PM
  4. Delete continuous form record
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 03-09-2012, 03:00 PM
  5. VBA delete current record continuous form
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 02-17-2012, 12:57 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