简体   繁体   中英

microsoft access populate foreign key in subform

So I've been reading everything that I can find on this problem and everyone's answer seems to be alternatives, but those alternatives won't really work for me.

What I am trying to do is click a button in form-1 and have it open form-2 with the correct foreign key from form-1 so I can view the data and add more based off of form-1's key. The reason for this is form-1 doesn't have enough space for hold all of the subforms that I would like to use. Also, putting a button on form-1 to make form-2 visible doesn't really work well for me, but looks to be my only option for now.

What happens now is when I click the button on form-1, form-2 does open, but it shows all of the data from table-2 and if I try to add a new record, I have to manually enter it in. I can't get the foreign key in form-2 to auto populate the ID in form-1 when using a button to call the form.

example tables and forms:
table-1 & form-1: The form has the same data as table
id
first name
last name

table-2 & form-2: The form has the same data as table
id
foreign key = table-1.id
address
someotherdata

goal: form-1 has a button called "add data". It opens form-2 with foreign key from the same ID from form-1. I can view past records or enter more data and save. If I go to form-1 record ID=5 and click the button, then form-2 will pop up with foreign key 5 and allow me to enter more data to foreign key 5/ID 5.

If you have any questions, please let me know. I can't really do much until this is figured out. I feel like I'm missing something and this should be much easier to do.

Thank you!!

Consider:

Code behind form to open another form to a new record and pass value with OpenArgs:
DoCmd.OpenForm "yourFormName", , , , acFormAdd, acDialog, Me.yourControlName

Code behind called form to pull value from OpenArgs and populate field:
If Me.NewRecord And Not IsNull(Me.OpenArgs) Then Me.yourControlName = Me.OpenArgs
Real trick is figuring out what event to put this in - probably the form Open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM