Error 3021 when deleting item from recordset

大兔子大兔子 提交于 2019-12-24 18:10:51

问题


When I use the code below, I sometimes receive a Error 3021. This only happens when I have one record in the recordset. Can you please tell me why, and how to fix it? It seems I've tried everything!

Thanks

Private Sub cmdDelSelectedAction_Click()

response = MsgBox("Are you sure?", vbYesNo, "Confirmation required")
If response = vbNo Then Exit Sub

If Me.[Arrangement-Actions subform].Form.Recordset.EOF Then
    Me.[Arrangement-Actions subform].Form.Recordset.MovePrevious
End If

If Me.[Arrangement-Actions subform].Form.Recordset.BOF Then
    Me.[Arrangement-Actions subform].Form.Recordset.MoveNext
End If

Me.[Arrangement-Actions subform].Form.Recordset.Delete
Me.[Arrangement-Actions subform].Form.Recordset.MoveNext

End Sub

回答1:


It's been a while, but I think the code would look like this:

Private Sub cmdDelSelectedAction_Click()
  Dim rec As Recordset = Me.[Arrangement-Actions subform].Form.Recordset
  If Not rec.BOF Or Not rec.EOF Then
    If MsgBox("Are you sure?", vbYesNo, "Confirm") = vbYes Then
      rec.Delete
    End If
  End If
End Sub

I find it a little odd that in your code you would ask to confirm to delete the record, and then before deleting the record, you would perform a MoveNext or a MovePrevious on the recordset. I would stay away from doing that since the end user might be deleting a different record than they were expecting.



来源:https://stackoverflow.com/questions/18082450/error-3021-when-deleting-item-from-recordset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!