在保存孩子之前强制创建父母 [英] forcing creation of parent before saving child

查看:52
本文介绍了在保存孩子之前强制创建父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决这个问题,并且

感谢一些帮助...


我有两张表,都有自动编号初级

键,并链接在传统的主孩子

关系中。我已经为那些

表创建了表单,并将子表格形式作为子表单插入到

主表格表单中。它的工作方式就像它应该的那样,因为我可以创建一个新的主人

记录,然后添加详细记录。


但是......主表中的所有列都有

的默认值,所以我真的不必在主表中输入

。 (这只是真正的目的

是提供一组细节记录。)但是,当然,

我去了一个新的记录,然后尝试进入一个细节

记录,没有创建一个主人

记录优先,Access抱怨没有价值

在子表格字段中链接到主人

表。我想做的是,能够将
转到子表格,创建一条记录,并且

Access创建必要的主记录(如果

详细记录之前,>
已经存在。


在子表单的Before_update过程中

我尝试修改主记录(Me.parent! -

[somefield] =< innocuous-value>,导致它创建
。然后我尝试保存它正在做什么

Me.parent.dirty = false。这会导致2115错误

来自Access(宏或函数设置为

BeforeUpdate [..]属性阻止访问

保存数据......)我尝试了很多不同的

排列,但没有找到对了

方式...(包括用

dao添加主记录 - 但表格对于

它一无所知所以没有显示它。还尝试添加

记录w ith dao,在表格的'记录集上但是

从那里得到错误。)


有什么方法可以强制创建一个<在保存

子记录之前,b / b
父记录(如果不存在)?


我希望这是可以理解的 - 如果没有请告诉

我和我会试着澄清......

I haven''t been able to figure this out and would
appreciate some help...

I have two tables, both with autonumber primary
keys, and linked in a conventional master-child
relationship. I''ve created forms for both those
tables, and inserted the child table form into the
master table form as a subform. It works just as
it is supposed to, in that I can create a new master
record, and then add detail records.

But... all the columns in the master table have
default values so I don''t really have to enter
anything in the master table. (It''s only real purpose
is to provide groups of detail records.) But, of course,
it I go to a new record, then try to enter a detail
record, without having created a master
record first, Access complains about no value
in the child table field that links to the master
table. What I want to do, is to be able to go
to the subform table, create a record, and have
Access create the necessary master record (if
none exists already) before trying to save the
detail record.

In the Before_update procedure in the child form
I tried modifying the master record (Me.parent!-
[somefield] = <innocuous-value>, causing it
to be created. Then I try to save it be doing
Me.parent.dirty = false. This causes a 2115 error
from Access ("The macro or function set to the
BeforeUpdate [..] property is preventing Access
from saving the data...") I tried many different
permutations of this without finding the right
way... (Including adding the master record with
dao -- but the form doesn''t know anything about
it so doesn''t display it. Also tried adding
the record with dao, on the form''s recordset but
get an error from that.)

Is there some way I can force the creation of a
parent record (if none exists) before saving the
child record?

I hope this is understandable -- if not please tell
me and I''ll try to clarify...

推荐答案

在用户进入之前的某个时刻要开始输入数据的子表单,必须使

主表单脏。所以Access知道它需要保存。

您可以在主表单上使用Me.Dirty = True来执行此操作。


周五,4月22日2005 23:59:29 -0600,Stuart McGraw

< sm ******** @ friizz.RimoovAllZZs.com>写道:
At some point before the user enters the subform to begin entering data, the
master form must be made "dirty" so that Access knows it needs to be saved.
You can do this using Me.Dirty = True on the master form.

On Fri, 22 Apr 2005 23:59:29 -0600, "Stuart McGraw"
<sm********@friizz.RimoovAllZZs.com> wrote:
我无法解决这个问题,并且会感谢一些帮助...

我有两张桌子,两者都带有autonumber主键,并与传统的主孩子关系链接。我已经为这两个表创建了表单,并将子表格形式作为子表单插入到主表格表格中。它就像它应该的那样工作,因为我可以创建一个新的主
记录,然后添加详细记录。

但是......所有列中的主表有默认值,所以我不必在主表中输入任何内容。 (这只是真正的目的
是提供一组细节记录。)但是,当然,它我去了一个新的记录,然后尝试输入一个细节
记录,没有先创建一个主要的记录,Access在子表格字段中抱怨没有值
链接到主表格。我想要做的是,能够在子窗体表中创建记录,并且在尝试之前创建必要的主记录(如果
不存在)保存
详细记录。

在子窗体的Before_update过程中
我尝试修改主记录(Me.parent! -
[somefield] =< ;无害值>,导致它被创建。然后我尝试保存它正在做
Me.parent.dirty = false。这导致来自Access的2115错误("设置为
BeforeUpdate [..]属性的宏或函数阻止Access
保存数据......)我尝试了许多不同的这种排列而没有找到正确的
方式......(包括用
dao添加主记录 - 但是表单对
没有任何了解,所以没有显示它。还尝试添加
带有dao的记录,在表格的'记录集上但是
从中得到错误。)

有什么方法我在保存
子记录之前可以强制创建一个父记录(如果不存在)吗?

我希望这是可以理解的 - 如果没有请告诉
我和我会试着澄清......
I haven''t been able to figure this out and would
appreciate some help...

I have two tables, both with autonumber primary
keys, and linked in a conventional master-child
relationship. I''ve created forms for both those
tables, and inserted the child table form into the
master table form as a subform. It works just as
it is supposed to, in that I can create a new master
record, and then add detail records.

But... all the columns in the master table have
default values so I don''t really have to enter
anything in the master table. (It''s only real purpose
is to provide groups of detail records.) But, of course,
it I go to a new record, then try to enter a detail
record, without having created a master
record first, Access complains about no value
in the child table field that links to the master
table. What I want to do, is to be able to go
to the subform table, create a record, and have
Access create the necessary master record (if
none exists already) before trying to save the
detail record.

In the Before_update procedure in the child form
I tried modifying the master record (Me.parent!-
[somefield] = <innocuous-value>, causing it
to be created. Then I try to save it be doing
Me.parent.dirty = false. This causes a 2115 error
from Access ("The macro or function set to the
BeforeUpdate [..] property is preventing Access
from saving the data...") I tried many different
permutations of this without finding the right
way... (Including adding the master record with
dao -- but the form doesn''t know anything about
it so doesn''t display it. Also tried adding
the record with dao, on the form''s recordset but
get an error from that.)

Is there some way I can force the creation of a
parent record (if none exists) before saving the
child record?

I hope this is understandable -- if not please tell
me and I''ll try to clarify...






Steve Jorgensen< no **** @ nospam .nospam>写在

新闻:2e ******************************** @ 4ax.com:
Steve Jorgensen <no****@nospam.nospam> wrote in
news:2e********************************@4ax.com:
在用户输入子表单开始输入数据之前的某个时刻,主表单必须是脏的。所以Access知道它需要保存。您可以在
主表单上使用Me.Dirty = True来执行此操作。
At some point before the user enters the subform to begin entering
data, the master form must be made "dirty" so that Access knows it
needs to be saved. You can do this using Me.Dirty = True on the
master form.




这是我不想使用连续或数据表的原因之一

用于数据输入的子表单,特别是不用于创建记录。


如果我这样做了,我只是禁用了
的能力
创建一条新记录,直到有父记录为止。

如果父记录中除了PK之外没有任何内容,那就是

所需的数据,然后我质疑架构是否需要

一些工作。也就是说,即使父记录*可以用

创建,只有PK,用户仍然必须返回并填写一些数据

以便记录有任何用处。所以,你现在已经在$ child *记录的创建结束时添加了一个条件,

并提出了强制用户如何强制执行的问题返回并修复

父记录。


这就是为什么我倾向于使用小的未绑定对话框来收集

用于创建主要记录的必填字段,所以我不会因为父/子表单布局中的问题而混淆




-

David W. Fenton http://www.bway.net/ ~dfenton

dfenton at bway dot net http: //www.bway.net/~dfassoc


" Stuart McGraw"在消息中写道:

新闻:11 ************* @ corp.supernews.com ...
"Stuart McGraw" wrote in message:
news:11*************@corp.supernews.com...
是有一些方法我可以在保存
子记录之前强制创建一个
父记录(如果不存在)?

我希望这是可以理解的 - 如果不是,请告诉我
我和我会试着澄清......
Is there some way I can force the creation of a
parent record (if none exists) before saving the
child record?

I hope this is understandable -- if not please tell
me and I''ll try to clarify...




这是MVP艾伦布朗非常好的过去这篇文章非常好

主题应该有助于我相信:



Here is a really good past post by MVP Allen Browne on this very
subject which should help I believe:

>>
基本的想法是你使用关系设计。

如果主表有一条记录,相关的表可以有很多。


主窗体绑定到主表。

子窗体绑定到相关表。


首先输入主窗体记录。当你输入子表格时,主

表格会保存到主表格中,如果还没有这样做的话。


当你在子表格中输入内容时,它保存到相关表中。如果

主窗体是新记录,则需要阻止子窗体记录从

保存。为此:

1.在设计视图中打开相关表格。

2.选择外键字段。

3.在在下窗格中,将其必需属性设置为是。

现在子窗体无法保存记录,除非外键从主窗体继承了

值。由于这发生在子表格

记录的条目末尾,如果主表单位于,则取消子表单的BeforeInsert

事件也很有帮助。一条新记录,因此当用户开始输入子表格记录时,用户会立即收到消息:


私有子Form_BeforeInsert(取消为整数)

如果是Me.Parent.NewRecord那么

取消= True

MsgBox"首先输入主表单记录。

结束如果

End Sub


使用每个表单的BeforeUpdate事件进行记录级验证。不要
尝试强制记录在此事件中保存:事件触发,因为

记录正在保存中。在此事件中,不要将焦点转移到

父/子:允许它完成保存。然后使用表格''

AfterUpdate事件切换,如果你有充分的理由这样做。


注意控件也有BeforeUpdate和AfterUpdate事件,但我们

在这里谈论表格的事件。


总之,使用Form_BeforeUpdate进行记录级验证,并允许

事件在尝试做其他事情之前完成。


HTH


-

Allen Browne - 微软MVP。珀斯,西澳大利亚。>>
>> The basic idea is that you use a relational design.
Where the primary table has one record, the related table can have many.

The main form is bound to the primary table.
The subform is bound to the related table.

You enter a main form record first. When you enter the subform, the main
form saves to the primary table if it has not already done so.

When you enter something in the subform, it saves to the related table. If
the main form is at a new record, you need to prevent a subform record from
saving. To do that:
1. Open the related table in design view.
2. Select the foreign key field.
3. In the lower pane, set its Required property to Yes.
Now the subform cannot save a record unless the foreign key has inherited a
value from the main form. Since this happens at the end of the subform
record''s entry, it is also helpful to cancel the subform''s BeforeInsert
event if the main form is at a new record, so the user gets the message as
soon as they start to enter a subform record:

Private Sub Form_BeforeInsert(Cancel As Integer)
If Me.Parent.NewRecord Then
Cancel = True
MsgBox "Enter the main form record first."
End If
End Sub

Use the BeforeUpdate event of each form for record-level validation. Do not
try to force the record to save in this event: the event fires because the
record is in the process of being saved. Do not shift focus to the
parent/child in this event: allow it to finish the save. Then use the form''s
AfterUpdate event to switch if you have a good reason for doing so.

Note that the controls also have BeforeUpdate and AfterUpdate events, but we
are talking about the events of the Form here.

In summary, use Form_BeforeUpdate for record-level validation, and allow the
event to complete before trying to do something else.

HTH

--
Allen Browne - Microsoft MVP. Perth, Western Australia.>>




希望有助于给你一些想法。

-

Jeff Conrad

访问Junkie

Bend,Oregon



Hope that helps to give you some ideas.
--
Jeff Conrad
Access Junkie
Bend, Oregon


这篇关于在保存孩子之前强制创建父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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