Andrea Sindico
Andrea Sindico

Reputation: 7440

ACCESS SubForm: trying to add records

I have a very simple access db with two tables linked by a 1-M relationship with a Reference Integrity constraint. I would like to manage data by means of two forms. The first shows each single record of the first table and provides a button opening the second form which shows only those records haging the referenced field with the value of the first table recods primary key.

The problem is that if I tries to add a new record in the second form I am warned the record field I use as reference is not set with a value contained in the keys of the primary table. However I expected this was done automatically by the Form when I tried to add a new record?

How should I fix this?

Upvotes: 0

Views: 859

Answers (1)

mwolfe02
mwolfe02

Reputation: 24207

If I understand correctly you are opening an independent second form (ie, not actually a subform) using some filtering criteria. One way to handle this would be to:

  1. add a hidden field to the second form bound to the foreign key field
  2. pass the value of the foreign key in as the OpenArgs argument of DoCmd.OpenForm when opening the second form
  3. in the Form_Open event, set the DefaultValue of the hidden foreign key field equal to the OpenArgs value you passed in step 2 (Me.HiddenKeyField.DefaultValue = Me.OpenArgs)

The other option is to make the second form a subform of the first. The easiest way to do that is to:

  1. go to design view for the first form and drag the second form from the database window onto the first form
  2. then go into the properties of the subform and set the Link Child Fields/Link Master Fields equal to the foreign key field

Upvotes: 2

Related Questions