简体   繁体   English

Microsoft Access在子窗体中填充外键

[英]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. 我想做的是单击Form-1中的一个按钮,并使用Form-1中的正确外键打开Form-2,以便我可以查看数据并根据Form-1的键添加更多内容。 The reason for this is form-1 doesn't have enough space for hold all of the subforms that I would like to use. 原因是form-1没有足够的空间容纳我要使用的所有子表单。 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. 另外,在form-1上放置一个按钮以使form-2可见对我来说并不是很好,但现在看来是我唯一的选择。

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. 现在发生的是,当我单击窗体1上的按钮时,窗体2确实打开了,但是它显示了表2中的所有数据,如果我尝试添加新记录,则必须手动输入它。使用按钮调用表格时,无法在表格2中获取外键来自动填充表格1中的ID。

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". 目标:form-1有一个名为“添加数据”的按钮。 It opens form-2 with foreign key from the same ID from form-1. 它使用来自Form-1的相同ID的外键打开Form-2。 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. 如果我转到表格1记录ID = 5并单击按钮,则表格2会弹出外键5,并允许我向外键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: 表单后面的代码可打开另一个表单以创建新记录并使用OpenArgs传递值:
DoCmd.OpenForm "yourFormName", , , , acFormAdd, acDialog, Me.yourControlName

Code behind called form to pull value from OpenArgs and populate field: 被调用表单后面的代码从OpenArgs中提取值并填充字段:
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. 真正的诀窍是弄清楚将其放入什么事件-可能是Open形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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