使用VBA在主窗体上的子子窗体上设置窗体属性 [英] Set Form Properties on SubSubform from the Main Form with VBA

查看:895
本文介绍了使用VBA在主窗体上的子子窗体上设置窗体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Access 2007(或2010)中,我需要使用VBA从主表单中设置一个嵌入到第一个子表单中的子表单的属性.为了弄清楚我要说的是哪种形式,我将此代码称为"subSubform",就像我在代码中一样(subSubform实际上是子子窗体控件的名称).

In Access 2007 (or 2010), I need to set the properties on a subform, embedded within the first subform, from the main form using VBA. To make it clear which form I'm talking about, I'll call the this form "subSubform" as I do in my code (well, subSubform is actually the name of the sub-subform control).

加载表单时,我在第一个子表单上有一个子表单控件,该控件为空白(不包含表单源对象).如果需要,我用有效表单的名称以及LinkMasterFields和LinkChildFields属性填写此控件的SourceObject属性.这一切似乎都可以正常工作,因为我确实得到了一个可扩展的加号,否则就不会出现.

When the form loads I have a subform control on my first subform that's blank (does not contain a form source object). If needed, I fill out the SourceObject property of this control with the name of a valid form, as well as the LinkMasterFields and LinkChildFields properties. This all appears to work fine because I do get an expandable plus sign that otherwise wouldn't be there.

接下来,我尝试为subSubform设置RecordSource,但出现错误:

Next I try to set the RecordSource for the subSubform and I get an error:

2455您输入的表达式对属性Form/Report的引用无效.

这是我正在使用的代码:

Here's the code I'm using:

Me!subform1.Form!subSubform.Form.RecordSource = sSubformSQL
'and I've tried this with the same bad results
Me.subform1.Form.subSubform.Form.RecordSource = sSubformSQL
'and this too
Forms("frmQuery").subform1.Form.subSubform!Form.RecordSource = sSubformSQL

'And I've tried this. It fails too, on the last line with the same error:
    Dim frm As Form
    Set frm = Forms("frmQuery").subform1.Form
    Dim frm2 As Form
    Set frm2 = frm.subSubform.Form

我尝试设置一个计时器并等待一秒钟以运行上述代码,以防万一加载表单需要花费一些时间.但这没什么区别.

I've tried setting a timer and waiting one second to run the above code, just in case it takes some time for the form to load. But this makes no difference.

我尝试从第一个子窗体而不是从主窗体运行代码,但这也无济于事.仍然失败,并出现相同的错误.

I've tried running code from the first subform instead of from the main form, but that doesn't help either. Still fails with the same error.

FWIW,我的子窗体和子子窗体都是DataSheet视图.我认为这对我在这里所做的事情没有任何影响(尽管我很清楚所涉及的性能问题,所以请不要对我大喊大叫).

FWIW, both my subform and my subSubform are DataSheet views. I don't think that makes any different on what I'm trying to do here (although I'm well aware of the performance problems involved so don't scream at me).

我知道这可能是一个非常奇怪的请求.但是我正在创建一个需要这个的动态查询界面.

I recognize that it's probably a very odd request. But I'm creating a dynamic query interface that needs this.

有什么想法如何在subSubform上设置表单属性?

Any ideas how to set form properties on a subSubform?

推荐答案

问题不在于您设置子表单的属性.格式:

The problem is not that you setting properites of a sub form. The format of:

me.subform1.form.subform2.form.RecordSource = sql

me.subform1.form.subform2.form.RecordSource = sql

s +应该可以正常工作.

s+Should work just fine.

问题是您正在尝试将continue表单(或数据表)嵌套在continu for(或数据表)的一侧.这是不允许的.

The problem is you are trying to nest a continues form (or datasheet) in side of a continues for (or a datasheet). This is NOT permitted.

解决方法是简单地将两个子表单并排放置,而不是嵌套.

The workaround is to simply place the two sub forms side by side and don’t' nest.

您仍然可以通过使用链接主设置来获取第二个(孩子)表格.

You can still get the second (child child) form to follow by using the link master settings.

在子级1的子级/主设置链接中,放置:

In the link child/master settings for child 1, you place:

linkChildFields main_id(无论该字段的名称是 用来关联到父表的子表单) LinkMasterFields [ID]

linkChildFields main_id (whatever is the name of the field in this sub-form that is used to relate back to the parent table) LinkMasterFields [ID]

在您放置的孩子2表单的链接孩子/主设置中

In the link child/master settings for child 2 form you place

linkChildFields main_id(无论该字段的名称是 用来关联到父表的子表单) LinkMasterFields [child1].[form].[ID]("masterForm"是 您用来保存主表单的控件.

linkChildFields main_id (whatever is the name of the field in this sub-form that is used to relate back to the parent table) LinkMasterFields [child1].[form].[ID] ("masterForm" is the name of the control you used to hold the master form.

因此,您可以对多对多进行建模,但不能嵌套,但是如上所述,无论如何,您还是可以得到相同的结果.因此,表单将如下所示:

So you can model a many to many, but you cannot nest, but as noted the above gives you the same result anyway. So a form will look like this one:

您还很可能在左侧(子代1)的当前事件中进行导航时希望对右侧进行重新查询-因为它通常不会自动为您更新.

You also likly in the on-current event of the left side (child 1), will want to place a requery of the right side when you navagate - as it often will not update automatic for you.

所以:

me.parent.Child2.Requery

这篇关于使用VBA在主窗体上的子子窗体上设置窗体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆