使用Access VBA检测新记录并获取ID [英] Detect new record and obtain ID using Access VBA

查看:361
本文介绍了使用Access VBA检测新记录并获取ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题有关,如何检测该表单用户在添加记录吗?如果我在表格的当前记录"变为新"时就知道了这一点,那将是理想的选择(导航到最后一个现有记录之外,或者单击新"导航按钮),但是只要第一条数据开始就知道输入将几乎一样好.我猜想这将在Form_Current()中测试某物,但是什么(或有更好的方法)?

Related to this question, how do I detect that a form user is adding a record? It would be ideal if I knew this as soon as the form's "current record" became "new" (navigating beyond the last existing record, or clicking the "new" navigation button), but knowing as soon as the first piece of data begins to be entered would be almost as good. I'm guessing that this would be testing something in Form_Current(), but what (or is there a better way)?

进一步相关,我最早能找到新记录的(自动编号)ID吗?这需要保存,不是吗?它会自动填充到表单的记录集中吗? (我知道这不是不是的代码驱动保存.AddNew ... .Update.)我要求这样做的原因是开始在子表单后面填充子表(数据是这样的,可以自动创建用户可以确认或修改的默认"数据集.

Further related, what is the earliest that I can find the (Autonumber) ID for the new record? That requires a save, does it not? Is it automatically populated into the form's recordset? (I know this is not the case for code-driven saves .AddNew ... .Update.) The reason I'm asking for this is to begin populating the child table behind the subform (the data is such that I can auto-create a "default" data set that the user can confirm or modify).

推荐答案

我认为您想要:

Private Sub Form_Current()
    If Me.NewRecord Then

End Sub

在保存记录之前,不应将自动编号与子表一起使用,否则(应该)遇到完整性问题.当子窗体成为焦点时,主表的自动编号将被保存.不要忘记,您可以对子窗体上的链接子项,链接主字段做很多事情.

The autonumber should not be used with a child table until the record has been saved, or you will (should) run into integrity problems. When the subform gets focus, the autonumber of the main table will have been saved. Do not forget that you can do a lot with link child, link master fields on a subform.

这篇关于使用Access VBA检测新记录并获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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