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

查看:58
本文介绍了父窗体上的新记录时子窗体ODBC错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个包含子表单的表单。两者都使用表单向导进行了creetd,并且受到IXO_NR列(在两个不同的表上)的约束,这是父表和子表单上文本框的控件源。


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


我试图将其重置为多个方式和我一直得到一个ODBC错误,我假设,因为一个值填充在子窗体的文本框中,Access解释说,在子窗体中也添加了一个新记录。由于子表单有几个非空列,因此生成了ODBC错误。


在父表单的'FORM_CURRENT事件中:


如果我!txtInfoXchgObjNr> 0然后

Me!subfrmIXODesc!txtInfoXchgObjNr.DefaultValue = Me!txtInfoXchgObjNr

Me!subfrmIXODesc.Requery

Else

''我!subfrmIXODesc!txtInfoXchgObjNr.Undo

''我!subfrmIXODesc!txtInfoXchgObjNr.Value = 0

''等

结束如果


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


谢谢,Ed。

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.

推荐答案

您在此方案中错误地引用了子表单控件。它应该是


如果我!.xtInfoXchgObjNr> 0然后

Me.subfrmIXODesc.form.xtInfoXchgObjNr.DefaultValue = Me.txtInfoXchgObjNr

Me.subfrmIXODesc.formRequery

Else

''Me.subfrmIXODesc.form.xtInfoXchgObjNr.Undo

''Me.subfrmIXODesc!txtInfoXchgObjNr.Value = 0

''等

结束如果


但是我不确定这是否能解决你的问题。请告诉我们。

J
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

展开 | 选择 | Wrap | 行号



嗨J,


感谢您的回复!


我所拥有的代码和您提供的代码都可以填充文本框并检索相关记录在子表单中。我在搜索解决方案时找到了代码。你的方法更有效吗?

当我尝试将子表单中的文本框重置为null时,ELSE中会生成ODBC错误。添加新记录时的值,以及它被注释掉的原因。


如果我在主表单在新记录上时没有重置子表单的文本框,则会填充不正确的值,如果输入的数据是子表单并保存,它会将子表单的数据与错误的PK相关联。


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


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


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


我相信你可以告诉我,我不熟悉Access及其对象,方法等。如果你碰巧有任何参考我可以请放心,请随时告诉我。


再次感谢所有帮助表示赞赏!


Ed。


PS抱歉这么长的回复:-)
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 :-)



除非您实际设置焦点并输入一些数据,否则不会保存子窗体记录。默认条目和假定的非空值,即使它们出现也是如此。在子表单中,没有承诺。我相信Undo是导致错误的原因......但是我无法在你的情况下测试它。你不应该做你正在做的事情。


作为测试,注释掉你的代码,进入你的主表单并输入测试记录。您可能会看到子窗体中的数据,但是......不用担心它。


保存主窗体记录。


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


告诉我。

J


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


这篇关于父窗体上的新记录时子窗体ODBC错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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