简体   繁体   English

MS-Access形式:奇怪的行为

[英]MS-Access form: weird behavior

I have a bound form with a subform bound to a different recordSource, although these recordSources can be linked through a field. 我有一个绑定窗体,其中一个子窗体绑定到另一个recordSource,尽管这些recordSources可以通过字段链接。

I have an unbound text-box control in my main form and a button control, with custom vba code attached to its OnClick event. 我的主窗体中有一个未绑定的文本框控件,还有一个按钮控件,在其OnClick事件上附加了自定义vba代码。

What I want to achieve is something like this: (pseudo-code for easier reading) 我想要实现的是这样的:(伪代码以便于阅读)

private sub button_Click()
    'Find the record with the content of the text-box control as its value in FieldA
    find(mainForm.RecordSource, fieldA, text-box.value) 
    if not found then
    'Create a record in the subform.recordSource with PK value = text-box.value
        createDefaultRecord(subform.Form.RecordSource,text-box.value)
    end if
    'load the values in the record in the main record source onto the record in the subform recordSource
    Me!subform.Form.value = mainForm.recordThatMatched.value
Exit Sub 

And I actually get it to work, but with a minor inconvenience: if I introduce a code that has not yet generated its record in subform.RecordSource, it creates it and then fails to load it BUT if you try to load a different code or close the form and then reload the first code again, IT WORKS! 实际上我得到它的工作,但有一个小麻烦:如果我介绍,目前尚未产生其纪录 subform.RecordSource 代码 ,它创建它,然后加载失败如果你尝试加载不同的代码或关闭表单 ,然后重新加载第一个代码这很有效!

Any idea as to why this is happening or how to code a work-around to this situation? 是否知道为什么会发生这种情况或如何为这种情况编写变通办法? It really is bugging me. 真的让我烦恼。 I have tried to run acSaveRecord and acSave before loading the record but the ending remains the same: I cannot load on the subform a record recently created without navigating first through the main form's records (which is kinda stupid and counter-intuitive) 我曾尝试在加载记录之前运行acSaveRecordacSave ,但是结尾保持不变:如果不先浏览主窗体的记录,就无法在子窗体上加载最近创建的记录(这有点愚蠢且违反直觉)

I may be wrong in my diagnosis and that would be OK to point out, too. 我的诊断可能有误,也可以指出这一点。 I don't have that much experience with Access and VBA. 我在Access和VBA方面没有太多经验。

Solution was to add this: 解决方案是添加以下内容:

Me!subform.Form.Requery 

after adding the record. 添加记录后。

A form (or subform) doesn't show newly added records in its recordsource unless it is requeried (manually with Shift+F9 or via code). 表单(或子表单)除非重新查询(手动使用Shift + F9或通过代码),否则不会在其记录源中显示新添加的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM