Access 怎么获取插入一条记录后的“自动编号”的id号
Access中用INSERTINTO语句插入一条记录后,怎么获取这条记录的“自动编号”的id号?我知道PHP+MYSQL环境中可以用mysql_insert_id()函数...
Access 中用 INSERT INTO语句插入一条记录后,怎么获取这条记录的“自动编号”的id号?
我知道PHP+MYSQL环境中可以用mysql_insert_id()函数获取。
哪位高手可以告诉我怎么可以在Access中获取这样的id号。 展开
我知道PHP+MYSQL环境中可以用mysql_insert_id()函数获取。
哪位高手可以告诉我怎么可以在Access中获取这样的id号。 展开
展开全部
方法一:
很简单,先取id后update
dim rcd as long
Dim rs As New ADODB.Recordset
rs.Open "表1", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
rs.AddNew
rcd=rs("id")
rs.update
rs.Close
docmd.runsql "update … where id="&str(rcd)
方法二:
必须 jet oledb 4 支持
Private Sub AutoIncTest()
Dim cnn As ADODB.Connection
'Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Set cnn = CurrentProject.Connection
'Set cmd = New ADODB.Command
'Set cmd.ActiveConnection = cnn
'cmd.CommandType = adCmdText
'cmd.CommandText = "INSERT INTO tblNewOrder2 " & _
"(ItemId, Quantity) VALUES (1, 20)"
'cmd.Execute
'Set cmd = Nothing
cnn.execute "insert into tblneworder2 (item) values ('dd')"
Set rst = New ADODB.Recordset
rst.Open "SELECT @@IDENTITY AS LastOrderId", _
cnn,
Options:=adCmdText
Debug.Print "OrderId for new record = " & _
rst("LastOrderId")
rst.Close
Set rst = Nothing
End Sub
很简单,先取id后update
dim rcd as long
Dim rs As New ADODB.Recordset
rs.Open "表1", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
rs.AddNew
rcd=rs("id")
rs.update
rs.Close
docmd.runsql "update … where id="&str(rcd)
方法二:
必须 jet oledb 4 支持
Private Sub AutoIncTest()
Dim cnn As ADODB.Connection
'Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Set cnn = CurrentProject.Connection
'Set cmd = New ADODB.Command
'Set cmd.ActiveConnection = cnn
'cmd.CommandType = adCmdText
'cmd.CommandText = "INSERT INTO tblNewOrder2 " & _
"(ItemId, Quantity) VALUES (1, 20)"
'cmd.Execute
'Set cmd = Nothing
cnn.execute "insert into tblneworder2 (item) values ('dd')"
Set rst = New ADODB.Recordset
rst.Open "SELECT @@IDENTITY AS LastOrderId", _
cnn,
Options:=adCmdText
Debug.Print "OrderId for new record = " & _
rst("LastOrderId")
rst.Close
Set rst = Nothing
End Sub
展开全部
呵,只要想通了会很简单的..
思路:
1.先把数据添加到数据库中
2.添加完成后,再执行一次查询最大ID的操作即可.
查询例:select max(id) from table 或select id from table order by id desc
思路:
1.先把数据添加到数据库中
2.添加完成后,再执行一次查询最大ID的操作即可.
查询例:select max(id) from table 或select id from table order by id desc
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询