MSAccess:子窗体记录源中的更改会导致LinkChildFields绑定丢失 [英] MSAccess: Change in subform recordsource causes loss of LinkChildFields binding

查看:224
本文介绍了MSAccess:子窗体记录源中的更改会导致LinkChildFields绑定丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完全被我迷住的问题.或更确切地说,我的解决方法很杂乱,我不明白为什么我想做的事不起作用:

I have a problem that Im completely stumped by. Or rather, I have a work-around that is messy and I don't understand why what I want to do doesn't work:

我有一个mainform/subform嵌套.子表单通过主/子字段链接(两种形式都不可编辑-因此Im直接使用recordsource字段而不是单独的控件).子表单是连续表单;子表单是连续表单.因此,当当前记录更改时,子窗体将在主窗体中显示与母版相关的记录.子表单基于查询-没有应用参数或过滤器-查询完全根据需要显示记录集.一切都很好(因为当主窗体的记录发生更改时,也要进行相关的子窗体记录).

I have a mainform / subform nest. The subform is linked by master/child fields (neither form is editable- so Im using the recordsource field directly rather than a separate control). The subform is a continuous form; so the subform displays the records related to the master in the mainform when the current record changes. The subform is based on a query- there are no parameters or filters being applied- the query presents the recordset exactly as needed. This all works perfectly (in that when the record of the mainform changes, so to do the related subform records).

当我希望子表单基于不同的不同查询(即基于不同的表)但与原始查询具有相同的唯一键("ID")和字段时,就会出现问题.这些查询都可以正常工作,并以表格形式显示它们也可以正常工作.

Problem arises when I want the subform to be based on a different a different query (ie based on different tables)- but with the same unique-key (the 'ID') and fields as the original query. The queries all work fine, and displaying them in the form works fine.

当我更改子表单的记录源时(从QueryA更改为QueryB),就会出现问题.子窗体的记录是正确的(即查询结果按预期显示)-但LinkChildFields不再起作用:子窗体不再显示查询中的相关记录-它显示了所有记录.换句话说,该表单显示整个记录集,就好像我没有设置LinkChild/LinkMaster一样.

The issue arises when I change the recordsource of the subform (so from QueryA to QueryB). The records the subform are correct (ie the query results are displayed as expected)- but the LinkChildFields is no longer functioning: the subform no longer shows the related records from the query- it displays them all. In other words, the form displays the entire recordset as if I had no LinkChild / LinkMaster set.

因此,MainForm中的切换控件的AfterUpdate事件中的代码为;

So, the code in the AfterUpdate event of the toggle control in the MainForm is ;

Private Sub optActRep_AfterUpdate()

Select Case Me.optActRep
    Case 1
        MainFormSubFormControl.Form.RecordSource = "QueryA"
    Case 2
        MainFormSubFormControl.Form.RecordSource = "QueryB"
End Select

已调试,并且属性"linkchldfields"仍正确设置("ID").香港专业教育学院试图在Recordsource更新之前将它们设置为vbNullString,并在Recordsource更新之后重置(相同的问题).奇怪的是,如果我更新了主窗体中的linkchildfield属性(在当前事件中),则该属性起作用并且似乎触发了.所以,

Ive debugged and the property 'linkchldfields' is still set correctly ('ID'). Ive tried setting these to vbNullString before the recordsource update and resetting after the recordsource update (same issue). The odd thing is if I update the linkchildfield property in the mainform (in the Current Event)- then it works and seems to fire. So,

Debug.print Me!Subform.LinkChildFields

正确返回"ID".我可以触发的唯一方法是通过编码;

correctly returns 'ID'. The only way I can make it fire is by coding;

Me!Subform.LinkChildFields = "ID"

进入Mainform的Current事件(即,每次MainForm记录更改时,都会显示正确的QueryB记录)...即使我只是将属性设置为我的调试告诉我它已经设置为.好像不再发射了.

into the Current event of the Mainform (ie so that every time the MainForm record changes, the correct QueryB record is displayed)... even though Im only setting the property to what my debug is telling me it is already set to. Its as if it no longer fires.

非常困惑.

推荐答案

访问继续执行最艰巨的任务.

Access continues to do the daftest things.

与表单设置完全无关.基础查询(QueryB)基于交叉表查询.尽管查询工作与表单无关(包括直接使用查询作为表单记录源时),但显然不喜欢将字段之一的乘积用作唯一键(即使它是相同的格式类型和分组的) /行,而不是交叉表字段.

Nothing to do with the form setup at all. The underlying query (QueryB) was based on a Crosstab query. Whilst the query worked fine independently of the form (including when directly using the query as the forms record source), it obviously didnt like using the product of one of the fields as the Unique key (even though it was the same format type and Grouped / Row rather than being a cross-tab field).

我将查询的交叉表部分更改为分组查询(使用IIF& Sum手动创建等效于交叉表的字段值),该表单现在可以正常工作了.

I changed the crosstab part of the query to a Grouped query (using IIF & Sum to manually create the Crosstab-equivalent field values) and the form now works fine.

我的经验教训-不要使用交叉表查询!为红鲱鱼道歉.

My lesson learnt- don't use Crosstab queries! Apologies for the red-herring.

这篇关于MSAccess:子窗体记录源中的更改会导致LinkChildFields绑定丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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