在子表单中输入数据时,MS Access 会自动在主表单中填写 ID [英] MS Access automatically fill id in main form when data is entered in subform

查看:38
本文介绍了在子表单中输入数据时,MS Access 会自动在主表单中填写 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MS Access 中,我建立了一个不寻常的关系.我正在显示一系列多边形中点的坐标.除了 id 之外,多边形不需要任何参考数据,但它们每个都可以有任意数量的坐标.所以我有两个表:坐标和多边形.Coordinates 有字段:coordinateID、x、y、order、PolygonID.多边形只有自动编号的 PolygonID 字段.

In MS Access, I have an unusual relationship set up. I am showing coordinates of points in a series of polygons. The polygons don't need any reference data other than an id, but they can each have any number of coordinates. So I have two tables: Coordinates and Polygons. Coordinates has fields: coordinateID, x, y, order, PolygonID. Polygons just has the PolygonID field which is autonumber.

Polygons                      Coordinates
--------                      -----------
PolygonID(primary key)--------PolygonID(foreign key)
                              coordinateID(primary key)
                              x
                              y
                              order

我为多边形创建了一个表单,并为坐标创建了一个子表单.我已经建立了具有参照完整性的父子关系.

I've created a form for Polygons with a subform for Coordinates. I've set up a parent child relationship with referential integrity.

问题是,Access 通常期望父表具有自动编号字段以外的一些数据,因此它在创建下一条记录之前等待数据输入.我可以键入序列中的下一个数字,Access 会创建记录,但如果我跳过该步骤而只在子窗体中输入数据,则父表中不会创建任何记录,最终会出现孤立条目.

The problem is, Access normally expects the parent table to have some data other than an autonumber field, so it waits for data entry before creating the next record. I can type the next number in the sequence, and Access creates the record, but if I skip that step and just enter data in the subform, no record is created in the parent table, and I end up with orphaned entries.

当我开始在子表中输入数据时,有没有办法强制 Access 创建父记录?

Is there a way to force Access to create the parent record when I start entering data in the child table?

推荐答案

这里有几个可能的问题...

There are a couple possible issues here...

首先,您需要确保在 PolygonID 字段上将父表单和子表单链接在一起.这可以通过子窗体向导完成,

First, you will want to make sure you are linking the parent and child forms together on the PolygonID field. This can be done with the SubForm Wizard,

或通过子表单对象上的属性.

or through the properties on the subform object.

要验证的第二件事是,在尝试在子表单中添加记录之前,您实际上已经在父表单中创建了一条新记录.

The second thing to verify is that you have actually created a new record in the parent form before you try to add a record in the subform.

为了说明这一点,您可以在 Polygons 表中添加一个 Description 字段.请注意,当您开始编辑说明时,自动编号字段会增加为一个数字.

To illustrate this, you can add a Description field to the Polygons table. Notice that the autonumber field increments to a number when you start editing the description.

现在,如果您为 x 输入坐标值,PolygonID 将自动填充.

Now if you enter a coordinate value for x, the PolygonID populates automatically.

通常您会先创建父记录,然后添加子记录.但在这种情况下,OP 想要从子表单上的新记录自动创建父记录之前父记录实际存在.

Normally you would create a parent record first, then add child records. But in this case the OP would like to create the parent record automatically from a new record on the child form before the parent record actually exists.

可以在子表单中使用一些 VBA 代码来完成此操作,但首先您需要向父表单的表中添加至少一个附加字段.然后下面的代码触发子表单中的 Form_Dirty 事件,一旦您开始添加新的子表单记录,就会发生该事件.它检查父窗体上的空 ID,如果找到,则假定这是父窗体上的新记录.从那里,它更改父表单上的字段值.这将创建新的父记录并通过父/子链接将 PolygonID 推送回子表单.

It is possible to do this using some VBA code in the child form, but first you need to add at least one additional field to the parent form's table. The following code then fires on the Form_Dirty event in the child form which happens as soon as you begin adding a new child form record. It checks for a null ID on the parent form, and if it finds one, it assumes this is a new record on the parent form. From there, it changes a field value on the parent form. This creates the new parent record and pushes the PolygonID back down to the child form through the parent/child link.

Private Sub Form_Dirty(Cancel As Integer)
    With Me.Parent
        If IsNull(!PolygonID) Then
            ' Change any field to create the parent record
            .Description = "Test"
            ' Save changes on the parent form.
            .Dirty = False
        End If
    End With
End Sub

同样,这不是父/子表单的典型使用场景,但它确实展示了一种从子表单创建相关父记录的方法.

Again, this isn't the typical usage scenario for parent/child forms, but it does demonstrate a way to create a related parent record from a child form.

这篇关于在子表单中输入数据时,MS Access 会自动在主表单中填写 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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