vba 从另一个子表单重新查询子表单,问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6015229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Requery subform from another subform, problems
提问by darkjh
I have 2 subforms on the main form. Sub_2 displays the details for the item chosen in sub_1, in a list. For both subform, I use the continue form, and they are bounded to 2 querys, with one field in common, say 'id'.
我在主窗体上有 2 个子窗体。Sub_2 在列表中显示在 sub_1 中选择的项目的详细信息。对于这两个子表单,我都使用 continue 表单,它们被限制为 2 个查询,其中一个字段是共同的,比如“id”。
What I have done is adding code in the DClick event on sub_1 to set the sub_2's filter like
我所做的是在 sub_1 上的 DClick 事件中添加代码来设置 sub_2 的过滤器,如
Me.Parent.sub_2.Form.Filter = "id=" & "'" & Me.Recordset!id & "'"
Then a requery
然后重新查询
Me.Parent.sub_2.Form.Requery
But no luck, when I double click the record in sub_1, no change in sub_2. I have had a look on relevant questions, I think this should work...
但是没有运气,当我双击 sub_1 中的记录时,sub_2 中没有变化。我看过相关问题,我认为这应该有效......
Any suggestions? Maybe I should use other approach to do this?
有什么建议?也许我应该使用其他方法来做到这一点?
PS: I'm sure Me.Parent.sub_2.Filter
is changed, but requery doesn't work.
PS:我确定Me.Parent.sub_2.Filter
已更改,但重新查询不起作用。
EDIT:
编辑:
Thanks for answers. I find the problem, that is, the 'id' field is in the query, but it's not displayed on the subform, so maybe Access cannot use it.
感谢您的回答。我找到了问题,即'id'字段在查询中,但它没有显示在子表单上,所以可能Access无法使用它。
And when the filter is changed, Access does a requery automatically.
当过滤器更改时,Access 会自动重新查询。
回答by John Mo
You should not need to do anything with the filter property.
您不需要对 filter 属性执行任何操作。
On the first subform, set the Link Master Fields and Link Child Fields properties as you would for a regular, single subform control on a form.
在第一个子窗体上,设置 Link Master Fields 和 Link Child Fields 属性,就像在窗体上设置常规的单个子窗体控件一样。
For the second subform, also set the Link Child Fields property as you would for a regular, single subform control. The Link Master Fields property will reference the control name of the first subform: [Subform 1 Control Name]!FieldName.
对于第二个子窗体,也像设置常规的单个子窗体控件一样设置 Link Child Fields 属性。Link Master Fields 属性将引用第一个子表单的控件名称:[Subform 1 Control Name]!FieldName。
In the OnCurrent event of the first subform control, add the following line of VBA code:
在第一个子窗体控件的 OnCurrent 事件中,添加以下 VBA 代码行:
Me.Parent.Controls![Subform 2 Control Name].Requery
Run the form. When you click a row in the first subform, the second subform will requery to display the child rows for the row selected in the first subform.
运行表单。当您单击第一个子窗体中的一行时,第二个子窗体将重新查询以显示第一个子窗体中所选行的子行。
回答by HansUp
Set the FilterOn property to True after you assign the Filter expression.
分配 Filter 表达式后,将 FilterOn 属性设置为 True。
Me.Parent.sub_2.Form.Filter = "id=" & "'" & Me.Recordset!id & "'"
Me.Parent.sub_2.Form.FilterOn = True
回答by HK1
You're going to need something like the following:
您将需要类似以下内容:
Me!Subform2.Form.Requery
'or
Forms!Mainform!Subform1.Form!Subform2.Form.Requery
I didn't have time to test this so I can't tell you for sure if Subform1 and Subform2 should be the name of the actual subform or merely the Subform control/container. I think it's the latter.
我没有时间对此进行测试,因此我无法确定 Subform1 和 Subform2 应该是实际子窗体的名称还是仅仅是子窗体控件/容器的名称。我认为是后者。
Here's an excellent source for these kinds of questions:
http://access.mvps.org/access/forms/frm0031.htm
这是此类问题的绝佳来源:http:
//access.mvps.org/access/forms/frm0031.htm
My only complaint with that page is that they do not cover calling methods, subs/functions, or referring to public variables. All of these are (or can be) slightly different than the scenarios listed there.
我对该页面的唯一抱怨是它们不包括调用方法、子/函数或引用公共变量。所有这些都(或可能)与此处列出的场景略有不同。