首页 > 解决方案 > 在与请求的名称或序号相对应的集合中找不到项目

问题描述

尝试自动生成销售 ID。但我不知道为什么我会收到这个错误。

Set qs = New ADODB.Recordset
qs.Open "select * from DailyRecords ", db, 3, 2
With qs
   If .RecordCount = 0 Then
      txtsalesID.Text = "S-0001"
   Else
      qs.MoveLast
      txtsalesID.Text = "S-" & Format(Right(qs!SalesID, 4) + 1, "0000") // this line is producing the error
   End If

我什至检查了数据库字段名称。它与 SalesID 相同。我尝试了很多并寻求帮助

标签: ms-accessvb6

解决方案


我认为问题在于您RecordCount用来检查记录集是否为空。将RecordCount光标留在记录集的末尾,因此即使有记录,您也无法从记录中读取数据。您应该EOF改用:

   If .EOF Then
      txtsalesID.Text = "S-0001"
   Else
      qs.MoveLast
      txtsalesID.Text = "S-" & Format(Right(qs!SalesID, 4) + 1, "0000")
   End If

推荐阅读