Check if subform has record

  • Thread starter Thread starter SF
  • Start date Start date
S

SF

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
 
The easiest way is to control the underlying table. Set to Required field to
Yes.

A user cannot save if no values is entered.
 
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
 
Back
Top