使用 Access VBA 检测新记录并获取 ID

发布于 2024-10-06 22:05:05 字数 462 浏览 0 评论 0原文

此问题相关,如何检测表单用户正在添加记录?如果我在表单的“当前记录”变为“新”时就知道这一点(导航到最后一个现有记录之外,或单击“新”导航按钮),但在第一个数据开始时就知道,那就太理想了被输入几乎同样好。我猜这将在 Form_Current() 中测试某些东西,但是什么(或者有更好的方法)?

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

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)?

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2024-10-13 22:05:05

我认为您想要:

Private Sub Form_Current()
    If Me.NewRecord Then

End Sub

在保存记录之前,不应将自动编号与子表一起使用,否则您将(应该)遇到完整性问题。当子表单获得焦点时,主表的自动编号将被保存。不要忘记,您可以使用子表单上的链接子字段、链接主字段执行很多操作。

I think you want :

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文