本文介绍了访问:在DAO中获取新创建的自动编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DAO中有一个代码可以连接到SQL Server 2008中的链接表.我需要在.AddNew上获取新创建的自动编号.

I have a code in DAO that connects to a linked table in SQL Server 2008. I need to get the newly created auto number on .AddNew.

Set db = CurrentDb
Set rs = db.OpenRecordset("AuditTrail")

rs.AddNew
rs("ActionID") = actionAdd
rs("dtDateTime") = Now()
rs("FormName") = frmName
rs("TableName") = tblName
rs("RecordID") = actionAdd
rs("Comment") = Nz(comment, "")
rs("UserID") = UserIDName
rs("UsernamePC") = VBA.Environ("USERDOMAIN")
rs("DomainPC") = VBA.Environ("USERDOMAIN")
rs("ComputerNamePC") = VBA.Environ("COMPUTERNAME")
rs.Update

rs.Close

如果在rs.Close之前使用rs("AuditTrailID"),它将返回1(第一项).

If I use rs("AuditTrailID") before rs.Close, it returns 1 (the first entry).

推荐答案

Bookmark属性设置为与LastModified属性相等,以返回到刚刚添加的记录.

Set the Bookmark property equal to the LastModified property to go back to the record you just added.

如Conrad Frix所述,在打开记录集时使用dbSeeChanges选项:

As Conrad Frix noted, use the dbSeeChanges option when opening the recordset:

Set db = CurrentDb
Set rs = db.OpenRecordset(Name:="AuditTrail", Options:=dbSeeChanges)

rs.AddNew
rs("ActionID") = actionAdd
' ... update additional fields
rs.Update
rs.Bookmark = rs.LastModified
Debug.Print rs("ID")
rs.Close

这篇关于访问:在DAO中获取新创建的自动编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:06