Sql server 在MS Access中SQL插入后在消息框中引用新创建的id
我在MS Access中有一个VBA脚本,它根据表单上的一些输入将新记录插入SQL Server表中Sql server 在MS Access中SQL插入后在消息框中引用新创建的id,sql-server,vba,ms-access-2010,Sql Server,Vba,Ms Access 2010,我在MS Access中有一个VBA脚本,它根据表单上的一些输入将新记录插入SQL Server表中 Private Sub Allocate_Click() Dim strSQL As String strSQL = "INSERT INTO dbo_ALLOCATIONS " _ & "([Employee No],[Device Serial Number],[Start Date]) VALUES " _ & "('" & Me.EmployeeNumb
Private Sub Allocate_Click()
Dim strSQL As String
strSQL = "INSERT INTO dbo_ALLOCATIONS " _
& "([Employee No],[Device Serial Number],[Start Date]) VALUES " _
& "('" & Me.EmployeeNumber & "', '" & Me.SerialNumber & "', '" & Me.StartDate & "')"
CurrentDb.Execute strSQL, dbFailOnError
Dim ReceiptNo As dao.Recordset
Set ReceiptNo = CurrentDb.OpenRecordset("select @@identity")
MsgBox "Device Successfully Allocated. Receipt ID is " & CurrentDb.OpenRecordset("select @@identity")
End Sub
最后,我查询在插入过程中自动递增的ID。然后我想在一个消息框中引用它,以便用户可以在其他地方使用它。我怎样才能让它在MsgBox命令中这样引用?我现在使用的这个命令导致了很多问题,因为我不能组合这个命令,当只使用“ReceiptNo”时,它会说它不是字符串。不可能打印整个记录集。您需要引用要打印的列,并在未到达文件结尾时循环记录集。然后将列作为字符串输出。但是我没有测试它
While not ReceiptNo.EOF
Msgbox Str(ReceiptNo!Identity)
ReceiptNo.moveNext
Wend
无法打印整个记录集。您需要引用要打印的列,并在未到达文件结尾时循环记录集。然后将列作为字符串输出。但是我没有测试它
While not ReceiptNo.EOF
Msgbox Str(ReceiptNo!Identity)
ReceiptNo.moveNext
Wend
记录集中应该只有一个结果。尝试将最后一行更改为:
MsgBox "Device Successfully Allocated. Receipt ID is " & ReceiptNo(0)
记录集中应该只有一个结果。尝试将最后一行更改为:
MsgBox "Device Successfully Allocated. Receipt ID is " & ReceiptNo(0)
这完全符合要求。我之前已经添加了ReceiptNo,但是没有添加列号选择,因为它给出了所需的结果。这完全符合要求。我之前已经添加了ReceiptNo,但是没有添加提供所需结果的列号选择