Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262

    Delete multiple records in a subform

    I have a form set up with a subform and a Delete function attached to a button. Right now, it only deletes one record at a time. How can I code this to make it delete highlighted records?

    Delete function Code:



    Code:
    Private Sub butDelete_Click()
    
    
    'check for existing record
        If Not (Me.frmEntrySub.Form.Recordset.EOF And Me.frmEntrySub.Form.Recordset.BOF) Then
        
            If MsgBox("Are you sure you want to delete?", vbYesNo) = vbYes Then
    'yes on confirm: delete
                CurrentDb.Execute "DELETE FROM EntryFormTable " & _
                " WHERE Component='" & Me.frmEntrySub!Component & "'"
    'refresh form
                Me.frmEntrySub.Form.Requery
            End If
        End If
        
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    I do this with a subform in datasheet view and Delete button on main form. Here is an extract of my code behind main form:

    Code:
    Option Compare Database
    Option Explicit
    Dim intHeight As Integer    'stores value for number of tests selected for deletion
    Dim intTop As Integer   'stores value for position of the first selected record in Tests recordset
    
    Private Sub ctrTests_Exit(Cancel As Integer)
    'sets module variables starting values for use in deleting selected tests
    intHeight = Me.ctrTests.Form.SelHeight
    intTop = Me.ctrTests.Form.SelTop
    End Sub
    
    Private Sub RemoveTests_Click()
    Dim N As Integer
    With Me.ctrTests.Form.RecordsetClone
    If .RecordCount < 1 Then
        MsgBox "No tests have been saved.  Delete action canceled.", , "RemoveTest Error"
    ElseIf intHeight < 1 Then
        MsgBox "No tests have been selected.  Delete action canceled.", , "RemoveTest Error"
    ElseIf MsgBox("This action may delete any saved test data.  Proceed?", vbExclamation + vbOKCancel, "Delete Test?") = vbOK Then
       For N = 1 To intHeight
          .AbsolutePosition = intTop - 1    'AbsolutePosition property is 0 based counter so must -1 to get position within the recordset
          .MoveFirst
          CurrentDb.Execute "DELETE FROM Tests WHERE LabNum='" & Me.tbxLABNUM & "' AND TestNum='" & !TestNum & "'"
          Me.ctrTests.Requery
       Next
    End If 
    End With
    End Sub
    Code behind the subform to disable keyboard delete:
    Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
    'suppress default Delete Confirm dialog box.
    Response = acDataErrContinue
    'cancel the automatic delete operation
    Cancel = True
    MsgBox "Must click Remove Test button to delete test from sample."
    End Sub

    Making me look at this again has me thinking about changing so the delete code is behind the subform and the main form button calls the subform procedure.
    Last edited by June7; 07-03-2014 at 11:24 AM.
    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
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    The problem with my Edit and Delete functions right now is it is dependent on the Component Tag. Since I got my alternates working (which must allow for duplicate components), it will edit/delete all records with that component, which is a logical error. Id rather delete and Edit records based on a highlighted record independent of the component tag.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    Is there a unique record ID? An autonumber field could serve that purpose.
    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.

  5. #5
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Yeah but the only purpose that serves right now is to keep order by entry. At some point I'm going to implement code to reset the count to 1 for every assembly or quote.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    Can probably use the ID to uniquely identify record for deletion.
    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.

  7. #7
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Where would those ID references go in your code?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    Try:

    CurrentDb.Execute "DELETE FROM EntryFormTable WHERE ID=" & !ID
    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.

  9. #9
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Got a run-time error '3167': Record is deleted.

    Code:
    Code:
    Private Sub butDelete_Click()
    
    
    With Me.frmEntrySub.Form.RecordsetClone
    If .RecordCount < 1 Then
        MsgBox "No tests have been saved.  Delete action canceled.", , "DeleteRecord Error"
    ElseIf intHeight < 1 Then
        MsgBox "No tests have been selected.  Delete action canceled.", , "DeleteRecord Error"
    ElseIf MsgBox("This action may delete any saved test data.  Proceed?", vbExclamation + vbOKCancel, "Delete Record?") = vbOK Then
       For N = 1 To intHeight
          .AbsolutePosition = intTop - 1    'AbsolutePosition property is 0 based counter so must -1 to get position within the recordset
          .MoveFirst
          CurrentDb.Execute "DELETE FROM EntryFormTable WHERE ID=" & !ID
       Next
    End If
    End With
    End Sub

  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,972
    Don't know why that causes error. Is there more info in that error message? Is that its full text?
    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.

  11. #11
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Click image for larger version. 

Name:	err1.jpg 
Views:	16 
Size:	165.2 KB 
ID:	17082Click image for larger version. 

Name:	err2.jpg 
Views:	16 
Size:	199.0 KB 
ID:	17083

    This is the result. I tried to delete the highlighted lines.

    Click image for larger version. 

Name:	err3.jpg 
Views:	16 
Size:	139.9 KB 
ID:	17084

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    I had to delete a lot of lines from my code that would be irrelevant to you. Appears I should have kept one. I have edited my earlier post. After the DELETE action, try:

    Me.frmEntrySub.Requery
    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.

  13. #13
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Different run-time error '3420': Object invalid or no longer set.

    Code:
    Code:
    Private Sub butDelete_Click()
    
    
    With Me.frmEntrySub.Form.RecordsetClone
    If .RecordCount < 1 Then
        MsgBox "No tests have been saved.  Delete action canceled.", , "DeleteRecord Error"
    ElseIf intHeight < 1 Then
        MsgBox "No tests have been selected.  Delete action canceled.", , "DeleteRecord Error"
    ElseIf MsgBox("This action may delete any saved test data.  Proceed?", vbExclamation + vbOKCancel, "Delete Record?") = vbOK Then
       For N = 1 To intHeight
          .AbsolutePosition = intTop - 1    'AbsolutePosition property is 0 based counter so must -1 to get position within the recordset
          .MoveFirst
          CurrentDb.Execute "DELETE FROM EntryFormTable WHERE ID=" & !ID
          Me.frmEntrySub.Requery
       Next
    End If
    End With
    End Sub
    Click image for larger version. 

Name:	err4.jpg 
Views:	16 
Size:	176.5 KB 
ID:	17096Click image for larger version. 

Name:	err5.jpg 
Views:	16 
Size:	185.7 KB 
ID:	17097Click image for larger version. 

Name:	err6.jpg 
Views:	16 
Size:	190.5 KB 
ID:	17098

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,972
    I suspect you did not declare intHeight and intTop in the module header as shown in my example.
    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.

  15. #15
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Code:
    Option Compare Database
    Dim intHeight As Integer    'stores value for number of records selected for deletion
    Dim intTop As Integer   'stores value for position of the first selected record in recordset
    Dim N As Integer
    Dim X As Double
    Dim Y As Double

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 07-03-2013, 10:02 AM
  2. delete multiple records at once
    By akrylik in forum Access
    Replies: 5
    Last Post: 04-22-2012, 08:13 AM
  3. Replies: 5
    Last Post: 01-24-2012, 06:19 PM
  4. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  5. Multiple Subform records
    By Lupson2011 in forum Access
    Replies: 2
    Last Post: 08-24-2011, 08:49 AM

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