4

我正在使用以下代码向表中添加一条记录:

Dim rs1 As DAO.Recordset
Set rs1 = CurrentDb.OpenRecordset("QUOTE-Run", dbOpenDynaset)

rs1.AddNew
rs1.Fields("[QuoteNumber]").value = [txtQuoteNumber].value
rs1.Fields("[LeadTime]").value = [txtLeadTime].value
rs1.Fields("[Qty]").value = [txtQty].value
rs1.Fields("[Title]").value = [txtTitle].value
rs1.Fields("[Date]").value = VBA.DateTime.Date
rs1.Fields("[Time]").value = VBA.DateTime.Time
rs1.Fields("[InitiatedBy]").value = Application.CurrentUser
rs1.Fields("[IncompleteProblemNotes]").value = [txtIncompleteProblemNotes].value
rs1.Fields("[CustomerNotes]").value = [txtCustomerNotes].value
rs1.Fields("[Memo]").value = [txtMemo].value
rs1.Fields("[Memo1]").value = [txtMemo1].value
rs1.Fields("[PrefferedQuoteRunSelect]").value = [txtPrefferedQuoteRunSelect].value
rs1.Fields("[CombinedRun]").value = [chkCombinedRun].value
rs1.Update

创建记录时,有一个名为 RunID 的自动编号主键字段会获取一个值。我需要知道该值是什么,以便稍后在代码中使用它。获取该价值的最简单方法是什么?

4

2 回答 2

3

我能想到的获取 RunID 值的最简单方法是在更新记录之前声明一个变量并将该变量设置为等于该字段。像这样的东西:

   Dim rs1 As DAO.Recordset
   Set rs1 = CurrentDb.OpenRecordset("QUOTE-Run", dbOpenDynaset)
   Dim TempRunID As Long

   rs1.AddNew
   rs1.Fields("[QuoteNumber]").value = [txtQuoteNumber].value
   rs1.Fields("[LeadTime]").value = [txtLeadTime].value
   rs1.Fields("[Qty]").value = [txtQty].value
   rs1.Fields("[Title]").value = [txtTitle].value
   rs1.Fields("[Date]").value = VBA.DateTime.Date
   rs1.Fields("[Time]").value = VBA.DateTime.Time
   rs1.Fields("[InitiatedBy]").value = Application.CurrentUser
   rs1.Fields("[IncompleteProblemNotes]").value = [txtIncompleteProblemNotes].value
   rs1.Fields("[CustomerNotes]").value = [txtCustomerNotes].value
   rs1.Fields("[Memo]").value = [txtMemo].value
   rs1.Fields("[Memo1]").value = [txtMemo1].value
   rs1.Fields("[PrefferedQuoteRunSelect]").value = [txtPrefferedQuoteRunSelect].value
   rs1.Fields("[CombinedRun]").value = [chkCombinedRun].value

   TempRunID = rs1.Fields("[RunID]").value
   rs1.Update
于 2012-04-23T19:03:50.790 回答
1

您可以运行使用该函数的第二个查询

SELECT LAST_INSERT_ID();
于 2012-04-23T19:22:37.443 回答