HandleNotInList Subroutine - Access

The following VBA code is useful for handling the ComboBox_NotInList event handler.

{copytext|vbCode}
Sub HandleNotInList(NewData As String, Response As Integer, entityName As _
String, targetTable As String, targetField As String, Optional formName As String = "")
    
    If vbYes = MsgBox("Add '" & NewData & "' as a new " & entityName & "?", _
    vbYesNo + vbQuestion + vbDefaultButton1) Then
    
        Dim newValue As String
        newValue = "'" & Replace$(NewData, "'", "''") & "'"
    
        Application.CurrentDb.Execute "INSERT INTO [" & targetTable & "] ([" & _
               targetField & "]) VALUES (" & newValue & ");"
                            
        Response = acDataErrAdded
        
        If Len(formName) > 0 Then
        
            DoCmd.OpenForm formName, acNormal, , "[" & targetField & "] = " & newValue, , acDialog
            
        End If
    
    Else
        
        Response = acDataErrContinue
    
    End If

End Sub