Using Access 2010. I have two subforms from and to. I have two buttons on the main form, addRecord which adds a new record to to using values from from's currently selected record and from the main form, and deleteRecord which deletes to's currently selected record.
Here is the code for the two buttons:
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 gets requeried because some of its fields depend on whether or not there is a corresponding record in to.
The problem is that when I have one record in to, and then I add another one using addRecord, when I try to delete the first record with deleteRecord, I get the following error:
Run-time error '3021':
No current record.
I can select any other record in to besides the first one, and delete it; also, if to is empty and I add a single record to it, then I can delete it. And once I delete another record in to, I can select that first one and delete it.
How can I delete that first record without having to delete another one first?
EDIT: examining the recordset further in the debugger, when I get the No current record error, .BOF and .EOF are both False, but .AbsolutePosition is -1.