[英]Access VBA: A subform can't get values from other subform
I have 3 forms. 我有3种形式。 A form is the parent, and the others are inserted as subforms of the parent:
表单是父表单,其他表单作为父表单的子表单插入:
f_p form parent
/ \
f_c1 f_c2 forms childs 1 and 2
I have programmed the "Current" event in form f_c1, so when the user changes the selected row in f_c1, some data should be copied to f_c2: 我已将f_c1形式的“当前”事件编程为f_c1,因此,当用户更改f_c1中的选定行时,应将一些数据复制到f_c2:
Private Sub Form_Current()
Me.Parent("f_c2").Form("field_in_f_c2") = Me("field_in_f_c1")
End Sub
But that returns an error 2455: 但这会返回错误2455:
You specified an expression that contains a non valid reference to the Form/Report property.
The error seems to be in the ".Form" part, since this works: 该错误似乎在“ .Form”部分中,因为这可行:
Debug.Print Me.Parent("f_c2").Name
but this doesn't work: 但这不起作用:
Debug.Print Me.Parent("f_c2").Form.Name
In other words, it looks like it is not possible to get into a child of the parent. 换句话说,看起来不可能进入父母的孩子。
What can be happening? 会发生什么事?
Note: f_c1 is bound to the parent via a common field, f_c2 is not bound to the parent. 注意:f_c1通过公共字段绑定到父级,f_c2未绑定到父级。 I use f_c2 just as a scrollable container of controls.
我将f_c2用作控件的可滚动容器。
Note 2: I have made a test: In a new blank form ("f_p_test"), I have inserted the form f_c2 two times ("f_c2_a" and "f_c2_b"). 注意2:我进行了测试:在新的空白表格(“ f_p_test”)中,我两次插入了表格f_c2(“ f_c2_a”和“ f_c2_b”)。 I have configured the "Click" event on a checkbox of f_c2 (so the event is available in both f_c2_a and f_c2_b).
我已经在f_c2的复选框上配置了“ Click”事件(因此该事件在f_c2_a和f_c2_b中都可用)。 In this test, I can access f_c2_a fields from f_c2_b fields, and vice-versa.
在此测试中,我可以从f_c2_b字段访问f_c2_a字段,反之亦然。
Update : I've just tried a trick, but it doesn't work: I have added a button to the parent form, and asigned an event to it, so when I click the button, the values of "f_c2" textboxes are shown. 更新 :我只是尝试了一个技巧,但没有用:我在父窗体中添加了一个按钮,并为其分配了一个事件,因此当我单击该按钮时,将显示“ f_c2”文本框的值。 The results: If I manually click on the button, then the values are shown.
结果:如果我手动单击该按钮,则会显示这些值。 But, if I add a click simulation to the "Current" event of form f_c1, then the information is not shown, the same old error happens.
但是,如果将单击模拟添加到形式为f_c1的“当前”事件中,则不会显示该信息,也会发生相同的旧错误。 Incredible!
难以置信!
Update 2 : Solved, it was a race condition. 更新2 :解决,这是一个竞赛条件。 The parent form is opened by other form, with:
父表单由其他表单打开,其中包括:
DoCmd.OpenForm "f_p"
so the children (subforms f_c1 and f_c2) are opened just after the parent, but the children are not loaded at the same time. 因此,子项(子窗体f_c1和f_c2)在父项之后打开,但子项不会同时加载。 When child f_c1 is loaded, its "Current" event is run, and it tries to access to f_c2, but f_c2 is still not loaded, so the folloging code fails:
加载子级f_c1时,将运行其“当前”事件,并尝试访问f_c2,但仍未加载f_c2,因此跟踪代码将失败:
Private Sub Form_Current() ' "Current" event for f_c1
Dim f_c2 As Form
set f_c2 = Me.Parent("f_c2").Form ' f_c2 could be not loaded
' rest of code
End Sub
The solution is to detect that f_c2 is still not loaded: 解决方案是检测f_c2仍未加载:
Private Sub Form_Current() ' "Current" event for f_c1
Dim f_c2 As Form
On Error Resume Next
Set f_c2 = Me.Parent("f_c2").Form
If Err <> 0 Then ' if f_c2 is still not loaded, then return
Exit Sub
End If
On Error GoTo 0
' rest of code
End Sub
Fortunatelly, the f_c1's Form_Current() event is called two times automatically when the parent form f_p is opened: The first time, f_c2 is still not loaded; 很明显,打开父窗体f_p时,会自动两次调用f_c1的Form_Current()事件:第一次,f_c2仍未加载; the second time, f_c2 is already loaded, so f_c1 data is successfully copied to controls of f_c2.
第二次,f_c2已被加载,因此f_c1数据已成功复制到f_c2的控件中。
The subforms are sitting in subform (or child) control. 子窗体位于子窗体(或子窗体)控件中。
When you want to reference the subforms you must do it via the subform control. 当您要引用子表单时,必须通过子表单控件进行引用。
Instead of: Me.Parent("f_c2").Form("field_in_f_c2")
而不是:
Me.Parent("f_c2").Form("field_in_f_c2")
It should be: Me.Parent.<name of subform control>.Form.field_in_f_c2
它应该是:
Me.Parent.<name of subform control>.Form.field_in_f_c2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.