使用Access 2010.我有两个子表单from
和to
。我在主表单上有两个按钮,addRecord
使用来自to
当前所选记录和主表单的值from
向deleteRecord
添加新记录。to
删除Private Sub addRecord_Click()
Dim contactid As Long
Dim requestid As Long
Dim startDate As Date
contactid = Me.from_subform.Controls("contactID").Value
requestid = Me.ID.Value
startDate = Me.startDate.Value
With Me.to_subform.Form.RecordsetClone
.AddNew
!AEid = contactid
!requestid = requestid
!startDate = startDate
.Update
End With
Me.to_subform.Form.Requery
Me.from_subform.Form.Requery
End Sub
Private Sub deleteRecord_Click()
If Me.to_subform.Form.RecordsetClone.RecordCount = 0 Then
Exit Sub
End If
Me.to_subform.Form.Recordset.Delete
Me.to_subform.Form.Recordset.MoveNext
Me.from_subform.Form.Requery
End Sub
当前选择的记录。
以下是两个按钮的代码:
from
to
被重新获得,因为它的某些字段取决于to
中是否有相应的记录。
问题是当我在addRecord
中有一条记录,然后我使用deleteRecord
添加另一条记录时,当我尝试删除Run-time error '3021':
No current record.
的第一条记录时,我得到了以下错误:
to
除了第一个记录之外,我可以在to
中选择任何其他记录,并将其删除;另外,如果to
为空并且我添加了一条记录,那么我可以删除它。一旦我删除No current record
中的另一条记录,我就可以选择第一条并删除它。
如何删除第一条记录而不必先删除另一条记录?
编辑:在调试器中进一步检查记录集,当我收到.BOF
错误时,.EOF
和.AbsolutePosition
都为False,但{{1}}为-1。< / p>
答案 0 :(得分:0)
你可以尝试
Me.to_subform.Form.Recordset.MoveFirst
在删除之前,我可以建议添加和删除记录的更简单方法是通过SQL
Currentdb.execute "DELETE * FROM TheTable WHERE ContactId=" & contactId
Me.from_subform.Form.Requery
答案 1 :(得分:0)
确定。
在彻底搜索了3021相关的所有内容之后,我决定不再讨论从记录集中删除项目。相反,我使用以下代码删除当前选定的记录:
Private Sub deleteBtn_Click()
Dim db As dao.Database
Dim rs As Recordset
Dim id As Long
'test for an empty subform
Set rs = Me.to_subform.Form.RecordsetClone
If rs.RecordCount = 0 Then
Exit Sub
End If
'test for no record selected (i.e. cursor is on the new record line)
If Not IsNull(Me.to_subform.Controls("id").Value) Then
'get the ID value of the selected record
id = Me.to_subform.Controls("id").Value
'delete it from the source table
Set db = CurrentDb
db.Execute "delete * from toTable where id=" & id, dbFailOnError
'refresh both forms
Me.to_subform.Form.Requery
Me.from_subform.Form.Requery
Else
MsgBox "No record selected."
End If
End Sub
它的工作非常精彩。