I have a data entry form named [frm SAMPLING DATA]. The record source for the form is the table [tbl SAMPLING DATA]. The form also contains a subform [frm SAMPLING DATA subform] in datasheet view. The record source for the subform is a query [sqry SAMPLING DATA TEMP] which is linked to the table [tbl SAMPLING DATA TEMP].
The user enters data on the main form, clicks an 'Add' button and the data entered appears in the subform. After all data is entered for the current session, the user clicks another button to post the data to the main database and the subform is cleared.
The user has the ability to delete a record from the subform before posting to the main database by selecting the record in the datasheet view and clicking a 'Delete' button on the main form. First a message box appears, asking the user to confirm the delete.
This works fine until I attempt to delete the last remaining record in the datasheet view of the subform. When I attempt to delete the last record I get the runtime error 3021, 'No current record'.
Any ideas? Thanks (below is the code assigned to the 'Delete' button).
Private Sub cmd_DELETE_Click()
Dim dbs As DAO.Database
Dim rstTemp As DAO.Recordset
Set dbs = CurrentDb
Set rstTemp=Me.frm_SAMPLING_DATA_subform.Form.RecordsetClone
If Not rstTemp.RecordCount = 0 Then
rstTemp.Bookmark = Me.frm_SAMPLING_DATA_subform.Form.Bookmark
theDay = rstTemp.Fields("DAY").Value
theMonth = rstTemp.Fields("MONTH").Value
theDate = rstTemp.Fields("DATE").Value
theYear = rstTemp.Fields("YEAR").Value
Response = MsgBox("Are you sure you want to delete this record?" + Chr(13) + Chr(13) + _
Chr(13) + "DAY OF WEEK: " & theDay + Chr(13) + _
Chr(13) + "DATE: " & theMonth & " " + theDate & ", " + theYear + Chr(13) + _
Chr(13), vbYesNo + vbQuestion, "DELETE RECORD?")
If Response = vbNo Then
Exit Sub
End If
Me.frm_SAMPLING_DATA_subform.Form.Recordset.Delete
Me.Requery
End If
End Sub
The user enters data on the main form, clicks an 'Add' button and the data entered appears in the subform. After all data is entered for the current session, the user clicks another button to post the data to the main database and the subform is cleared.
The user has the ability to delete a record from the subform before posting to the main database by selecting the record in the datasheet view and clicking a 'Delete' button on the main form. First a message box appears, asking the user to confirm the delete.
This works fine until I attempt to delete the last remaining record in the datasheet view of the subform. When I attempt to delete the last record I get the runtime error 3021, 'No current record'.
Any ideas? Thanks (below is the code assigned to the 'Delete' button).
Private Sub cmd_DELETE_Click()
Dim dbs As DAO.Database
Dim rstTemp As DAO.Recordset
Set dbs = CurrentDb
Set rstTemp=Me.frm_SAMPLING_DATA_subform.Form.RecordsetClone
If Not rstTemp.RecordCount = 0 Then
rstTemp.Bookmark = Me.frm_SAMPLING_DATA_subform.Form.Bookmark
theDay = rstTemp.Fields("DAY").Value
theMonth = rstTemp.Fields("MONTH").Value
theDate = rstTemp.Fields("DATE").Value
theYear = rstTemp.Fields("YEAR").Value
Response = MsgBox("Are you sure you want to delete this record?" + Chr(13) + Chr(13) + _
Chr(13) + "DAY OF WEEK: " & theDay + Chr(13) + _
Chr(13) + "DATE: " & theMonth & " " + theDate & ", " + theYear + Chr(13) + _
Chr(13), vbYesNo + vbQuestion, "DELETE RECORD?")
If Response = vbNo Then
Exit Sub
End If
Me.frm_SAMPLING_DATA_subform.Form.Recordset.Delete
Me.Requery
End If
End Sub