MS Access keep ID field in subform filled with ID from parent form

穿精又带淫゛_ 提交于 2020-01-17 04:12:33

问题


Everytime I jump to where I can enter e new record, the ID field from the parent is empty and so the connection is lost. I am looking for a workaround.

My Setup

I have a parent form that deals with two 1:n relationships

(school-class --> pupils, school-class --> tests).

For the first relationship I used the wizard. Everything works find. For the second I show the connected tests in an unbound list. Under the list is a button opening the form for entering a new record (test) for the class I came from (parent form). So I filter the sub-form via VBA so that only the tests of the current class are shown. That works perfectly fine, too.

When moving through the tests already connected with the class the correct ID (of the class filtered) is the value of the corresponding input field. But when I come to the new new record state (all fields empty), then the connection to the parent breaks and the user has to manually enter the ID of the parent (school-class).

My Question

Is my setup correct? Is there a better way to create a subform that offers to (only) enter a new record connected to the parent data? (Maybe without the ID input field in the subform and passing of forcing the value via VBA?)

Thank you for your time!


回答1:


You can use Default Value to set the classid of Tests form but be sure the parent form is open in background or behind the pop up.

Under Property Sheet/Data tab of Tests form's classid control, enter in Default Value cell:

=Forms!parentformname!classid

Alternatively, in VBA in the Tests form's OnOpen Event:

Me.classid.DefaultValue = Forms!parentformname!classid

You can then choose to hide (Visible - No) this classid control so users do not modify it. It's always advised to never allow users control of primary and foreign keys.



来源:https://stackoverflow.com/questions/28991808/ms-access-keep-id-field-in-subform-filled-with-id-from-parent-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!