Hi,
I have frmMain with 2 subform (frmSubForm1 and frmSubForm2).
Is there a way to check if a subform has record before allow user to save?
SF
This code check if there are records in subform, when user enter data
in subform and cancel saving of new record. Use the copy on both
subforms, event BeforeUpdate:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim rst As Recordset
Set rst = Me.RecordsetClone
If rst.RecordCount > 0 Then
MsgBox "This subform has records"
Cancel = True 'Cancel Save
Me.Undo
End If
Set rst = Nothing
End Sub
This code check if there are records in both subforms and cancel save
of new record in parent form. Use it on parent form, event
BeforeUpdate:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim rst As Recordset
Set rst = frmSubForm1.Form.RecordsetClone
If rst.RecordCount > 0 Then
MsgBox "Subform1 has records"
Cancel = True
Me.Undo
End If
Set rst = frmSubForm2.Form.RecordsetClone
If rst.RecordCount > 0 Then
MsgBox "Subform2 has records"
Cancel = True
Me.Undo
End If
Set rst = Nothing
End Sub
Regards,
Branislav Mihaljev
Microsoft Access MVP