在父窗体上新建记录时出现子窗体ODBC错误[英] Subform ODBC Error When New Record On Parent Form

本文是小编为大家收集整理的关于在父窗体上新建记录时出现子窗体ODBC错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

你好,

我有一个包含子表单的表单.两者都是使用表单向导创建的,并由 IXO_NR 列(在两个不同的表上)绑定,该列是父表单和子表单上文本框的控制源.

目的是将子表单的 IXO_NR 默认为父表单的 IXO_NR,然后重新查询子表单以防止用户必须滚动浏览所有子表单的记录才能找到他们可能正在寻找的记录.如果父表单上有新记录,则应重置子表单上的 IXO_NR,因为它包含最后选择的记录中的值.

我尝试通过多种方式重置它,但我一直收到 ODBC 错误,因为我假设,因为在子表单的文本框中填充了一个值,Access 解释为在子表单中添加了一条新记录好.由于子表单有几个非空列,因此生成了 ODBC 错误.

在父窗体的 FORM_CURRENT 事件上:

如果我!txtInfoXchgObjNr >0 然后
Me!subfrmIXODesc!txtInfoXchgObjNr.DefaultValue = Me!txtInfoXchgObjNr
我!subfrmIXODesc.Requery
其他
'' Me!subfrmIXODesc!txtInfoXchgObjNr.Undo
'' 我!subfrmIXODesc!txtInfoXchgObjNr.Value = 0
''等
结束如果

那么如何重置子表单/子表单的文本框以防止这种情况发生呢?

谢谢, Ed.

推荐答案

在这种情况下,您错误地引用了您的子窗体控件.应该是

如果我!.xtInfoXchgObjNr >0 然后
Me.subfrmIXODesc.form.xtInfoXchgObjNr.DefaultValue = Me.txtInfoXchgObjNr
Me.subfrmIXODesc.formRequery
其他
'' Me.subfrmIXODesc.form.xtInfoXchgObjNr.Undo
'' Me.subfrmIXODesc!txtInfoXchgObjNr.Value = 0
''等
结束如果

但我不确定这是否能解决你的问题.让我们知道.
J

在这种情况下,您错误地引用了您的子窗体控件.它应该是

展开|选择span>|Wrap|行号

嗨,J,

谢谢回复!

我拥有的代码和您提供的代码都可以填充文本框并检索子表单中的相关记录.我在寻找解决方案时发现了该代码.你的方法效率更高吗?

当我尝试将子表单中的文本框重置为"null"时,会在 ELSE 中生成 ODBC 错误.添加新记录时的价值,这就是它被注释掉的原因.

如果在主表单位于新记录上时我不重置子表单的文本框,它将填充不正确的值,如果在子表单上输入并保存数据,它将关联子表单的数据到错误的PK.

例如,如果主表单有一条 PK 为 2 的现有记录,则子表单的文本框将填充 2,并且将检索任何相关数据(如果有).在新记录中,主窗体的对象为空白,但子窗体的文本框包含值 2,而不是空白.

发生 ODBC 错误是因为子表单不包含有效插入所需的所有信息(例如,无法将空值插入列...列不允许空值).如果填充并保存了所需的信息,它将与存储在文本框中的最后一个 PK 值相关联,在本例中为 2.

所以问题是,如何将子表单的文本框重置为"null"?值所以 Access 不会尝试插入记录并引发 ODBC 错误?

我相信你会说,我不熟悉 Access 及其对象、方法等.如果你碰巧有任何我可以研究的参考资料,请随时告诉我.

再次感谢,感谢所有帮助!

编.

附言抱歉这么长的回复:-)

除非您实际设置焦点并输入一些数据,否则不会保存子表单记录.默认条目和假定的 Not Null 值,即使它们"出现"在子表单中,未提交.我相信撤消是导致错误的原因..但是我无法在您的情况下进行测试.你不应该做你正在做的事.

作为一个测试,注释掉你拥有的代码,进入你的主窗体并输入一个测试记录.您可能会在子表单中看到数据,但是……不要打扰它.

保存主窗体记录.

现在转到子表单表并尝试查找 FK 记录.它不会/不应该存在.

让我知道.
J

本文地址:https://www.itbaoku.cn/post/1050475.html

问题描述

Hi,

I have a form which contains a subform. Both are were creetd using the form wizard and are bound by the column IXO_NR (on two different tables), which is the control source for a textbox on both the parent and subform.

The intent was to default the subform''s IXO_NR to the parent form''s IXO_NR, then requery the subform to prevent the user from having to scroll through all of the subform''s records to find the one they may be looking for. In the case of a new record on the parent form, IXO_NR on the subform should be reset since it contains the value from the last record selected.

I tried to reset it a number of ways and I kept getting an ODBC error as, I''m assuming, since a value was populated in the subform''s textbox, Access interpreted that a new record was being added in the subform as well. Since the subform has a couple of not null columns, an ODBC error was generated.

On the parent form''s FORM_CURRENT event:

If Me!txtInfoXchgObjNr > 0 Then
Me!subfrmIXODesc!txtInfoXchgObjNr.DefaultValue = Me!txtInfoXchgObjNr
Me!subfrmIXODesc.Requery
Else
'' Me!subfrmIXODesc!txtInfoXchgObjNr.Undo
'' Me!subfrmIXODesc!txtInfoXchgObjNr.Value = 0
'' etc.
End If

So how can I reset the subform / subform''s textbox to prevent this ?

Thanks, Ed.

推荐答案

you''re referencing your subform control incorrectly in this scenario. it should be

If Me!.xtInfoXchgObjNr > 0 Then
Me.subfrmIXODesc.form.xtInfoXchgObjNr.DefaultValue = Me.txtInfoXchgObjNr
Me.subfrmIXODesc.formRequery
Else
'' Me.subfrmIXODesc.form.xtInfoXchgObjNr.Undo
'' Me.subfrmIXODesc!txtInfoXchgObjNr.Value = 0
'' etc.
End If

But I''m not sure if that''s going to work to solve your problem. Let us know.
J

you''re referencing your subform control incorrectly in this scenario. it should be

Expand|Select|Wrap|Line Numbers

Hi J,

Thanks for the reply !

The code I had and the code you provided both worked as far as populating the textbox and retrieving the related records in the subform. I found that code when searching for a solution. Is you''re method more efficient ?

The ODBC error is generated in the ELSE when I try to reset the textbox in the subform to a "null" value when a new record is being added and that''s why it was commented out.

If I don''t reset the subform''s textbox when the main form is on a new record, it will be populated with an incorrect value and if data is entered on the subform and saved, it will associate the subform''s data to the wrong PK.

For example, if the main form had an existing record with a PK of 2 the subform''s textbox would be populated with 2 and any relevant data, if any, would be retrieved. On a new record, the main form''s objects are blank but the subform''s textbox contains the value of 2, instead of being blank as well.

The ODBC error occurred since the subform did not contain all of the required info for a valid insert (e.g. cannot insert a null value into columns...column does not allow nulls). If the required info was populated and saved, it''d be associated with the last PK value stored in the textbox, in this case 2.

So the question is, how do I reset the subform''s textbox to a "null" value so Access does not try to insert a record and throw the ODBC error ?

As I''m sure you can tell, I''m not familiar with Access and it''s objects, methods, etc. If you happen to have any references I could look into, please feel free to let me know.

Thanks again and all help is appreciated !

Ed.

P.S. Sorry for such a long reply :-)

The subform record wouldn''t be saved unless you actually set focus to it and enter some data. Default entries, and assumed Not Null values, even though they "appear" in the subform, aren''t committed. I believe the Undo is what''s causing the error..however I have no way to test that in your situation. You shouldn''t have to do what you''re doing.

As a test, comment out the code you have, go into your main form and enter a test record. You may see data in the subform, however...don''t bother with it.

Save the Main form record.

Now go to the subform table and try to find the FK record. It will not/should not be there.

Let me know.
J