Delete all records form Subform with Main Page Button

stu_c

Registered User.
Local time
Today, 16:07
Joined
Sep 20, 2007
Messages
494
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
 
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
 
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
 

Users who are viewing this thread

Back
Top Bottom