vba 在 Access 2007 中查找新记录的自动编号 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4413157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:24:39  来源:igfitidea点击:

Finding Autonumber ID for new record in Access 2007

ms-accessvbams-access-2007access-vba

提问by RolandTumble

The question I asked over herewas actually a two-part question. I accepted an answer that was perfect for the first part, but I'm still stumped on the second.

在这里问的问题实际上是一个两部分的问题。我接受了一个对第一部分来说是完美的答案,但我仍然对第二部分感到困惑。

I've got a form tied to a table. The user (still just me, in develop mode, but still...), enters a new record in the form. How do I get the (Autonumber) ID of that record? Even after forcing a save, it doesn't seem to be anywhere I know how to find it.

我有一个绑在桌子上的表格。用户(仍然只是我,处于开发模式,但仍然......)在表单中输入一个新记录。如何获取该记录的(自动编号)ID?即使在强制保存之后,它似乎也不在我知道如何找到它的任何地方。

The ID is not on the form anywhere, though it is in the underlying recordset. For that matter, the recordset seems to be pointing to the first record, not the newly-added one (I do know that this behavior is documented for pure-code DAO recordsets, but I'd expect the current record of form.recordset to be the one that's represented on the screen).

ID 不在表单的任何地方,尽管它在基础记录集中。就此而言,记录集似乎指向第一条记录,而不是新添加的记录(我确实知道这种行为是为纯代码 DAO 记录集记录的,但我希望 form.recordset 的当前记录能够成为屏幕上显示的那个)。

回答by Fionnuala

You can use the After Insert event:

您可以使用 After Insert 事件:

Private Sub Form_AfterInsert()
MsgBox Me.ID  ''Name of autonumber field
End Sub

回答by GaryH

You can also use the .movelast command

您也可以使用 .movelast 命令

Set rstTemp etc < rstTemp.addnew

设置 rstTemp 等 < rstTemp.addnew

populate fields

填充字段

rstTemp.update rsttemp.movelast

rstTemp.update rsttemp.movelast

The value of rstTemp!IDwhere ID is the autonumber field will be the autonumber value

的值rstTemp!ID,其中ID是自动编号字段将是自动编号值