In post#1, I mentioned that the supplied code was bare bones. Here's a more comprehensive real world capable version of the two functions.
Code:
Public Function CCAddNewRecord(frm As Form)
On Error GoTo Error_Handler
With frm
'if currently working on an unsaved new record
'and this function is called again, ignore.
If .NewRecord Then
Else
DoCmd.GoToRecord , , acNewRec
End If
End With
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
Select Case Err
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in function CCAddNewRecord, called by " & frm.Name
End Select
Resume Error_Handler_Exit
Resume
End Function
'---------------------------------------------------------------------------------------
' Method : CCDeleteRecord
' Author : davegri
' Date : 03/16/22
' Purpose: Delete record. This can be called from either a main form or a child form.
' Note that attempting to delete a main form record that has dependent child records
' will result in a runtime error. To avoid this, programmer is responsible to determine if
' that error will occur and not call this function at all, or handle the runtime error
' in this function's error handler.
' The runtime error can also be avoided by instituting cascade deletes via the
' relationships if appropriate.
'---------------------------------------------------------------------------------------
Public Function CCDeleteRecord(frm As Form)
On Error GoTo Error_Handler
With frm
'handle situation that user is trying to delete an
'unsaved new record.
If .NewRecord Then
frm.Undo
DoCmd.RunCommand acCmdRecordsGoToLast
Exit Function
End If
If MsgBox("Do you want to completely remove the displayed record?", vbInformation + vbYesNo) = vbNo Then
Exit Function
End If
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True
DoCmd.RunCommand acCmdRecordsGoToFirst
End With
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
Select Case Err
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in function CCDeleteRecord, called by " & frm.Name
End Select
Resume Error_Handler_Exit
Resume
End Function