MS-Access表单:奇怪的行为

时间:2016-11-29 14:18:21

标签: vba ms-access subform

我有一个绑定表单,其子表单绑定到不同的recordSource,尽管这些recordSource可以通过字段链接。

我的主窗体和按钮控件中有一个未绑定的文本框控件,其OnClick事件附加了自定义的vba代码。

我想要实现的是这样的:(伪代码以便于阅读)

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 

我实际上是让它工作,但带来一点小小的不便:如果我在subform.RecordSource中引入了尚未生成其记录的代码 ,它会创建如果您尝试加载其他代码或关闭表单,然后重新加载第一个代码 再次, IT工作!

任何想法为什么会发生这种情况或如何编写解决这种情况的方法?这真让我烦恼。我已经尝试在加载记录之前运行acSaveRecordacSave但结尾仍然是相同的:我无法在最近创建的记录上加载子表单而不首先浏览主表单的记录(这有点愚蠢并且反直觉)

我的诊断可能有误,也可以指出。我对Access和VBA没有太多经验。

1 个答案:

答案 0 :(得分:1)

解决方案是添加:

Me!subform.Form.Requery 

添加记录后。

表单(或子表单)不会在其记录源中显示新添加的记录,除非它被重新获取(使用Shift + F9或通过代码手动)。

相关问题