Subform Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using Access 2000 and have a form that contains several subforms.
Sometimes the user enters data into a subform when there is no record above
that to attach to. I would like an error message to pop up when a user tries
to do this. Any help would be appreciated. Thanks in advance. Paul
 
Good question. Even if you create a relationship and enforce referential
integrity, Access (correctly) allows entries where the foreign key is null,
so you do have block this possibility in most tables.

1. Open the subform's table in design view.
2. Select the foreign key field.
3. In the lower pane, set Required to Yes.

Now the user cannot enter a related in the subform when the main form is at
a new record. But they do not receive an error message until they have
finished entering the subform record and try to save it. To notify them
earlier, cancel the subform's BeforeInsert event:

Private Sub Form_BeforeInsert(Cancel As Integer)
If Me.Parent.NewRecord Then
Cancel = True
MsgBox "Enter the main form record first."
End If
End Sub
 
Back
Top