Delete all records form Subform with Main Page Button (1 Viewer)

stu_c

Registered User.
Local time
Today, 21:24
Joined
Sep 20, 2007
Messages
489
Hi all
I have the below, when "BtnDeleteAllRecords" is clicked from the main from I want to delete all records in the subform named "SFrmSubDetails", I tried doing a loop with the below code but no success, I have never done loops below so probably completely wrong, wasn't sure what I need to do, unless there is an easier way

FrmMainDetails - Main Form
BtnDeleteAllRecords - Main Form

SFrmSubDetails - Tabular Subform
TxtNameID - Subform AutoNumber
TxtForeNames - Subform Text Box
TxtSurname - Subform Text Box
TxtNameRecordCount - =Count([TxtNameID])

Code:
Private Sub BtnDeleteAllRecords_Click()
[Forms]![FrmMainDetails]![SFrmSubDetails].SetFocus
Do Until [Forms]![FrmMainDetails]![SFrmSubDetails].[Form]![TxtNameRecordCount] = 0
            DoCmd.SetWarnings False
            DoCmd.RunCommand acCmdDeleteRecord
            DoCmd.SetWarnings True
Loop
 

stu_c

Registered User.
Local time
Today, 21:24
Joined
Sep 20, 2007
Messages
489
solved!
Code:
Dim Records As DAO.Recordset

    Set Records = SFrmSubDetails.Form.RecordsetClone
    If Records.RecordCount > 0 Then
        Records.MoveFirst
        While Not Records.EOF
            Records.Delete
            Records.MoveNext
        Wend
    End If
    Records.Close

End Sub
 

Minty

AWF VIP
Local time
Today, 21:24
Joined
Jul 26, 2013
Messages
10,368
If you have a foreign key that identifies the subform records why not simply run a delete query rather than looping?
Code:
Dim strSQL as String

StrSQL = "DELETE * From YourSubFormRecordsTable WHERE ForeignKeyID = " & Me.ForeignKeyID
CurrentDb.Execute StrSQL
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:24
Joined
Feb 19, 2002
Messages
43,233
I want to delete all records in the subform named "SFrmSubDetails"
Forms don't hold records. Tables hold records. Minty gave you the correct solution.
 

Users who are viewing this thread

Top Bottom