Private Sub cboMakeList_NotInList(NewData As String, Response As Integer)
On Error GoTo Err_ErrorHandler
' provide text constants to reduce text later and allow for faster execution
' due to added speed from the compilation of constants
Const Message1 = "The data you have entered is not in the current selection."
Const Message2 = "Would you like to add it?"
Const Title = "Unknown entry..."
Const NL = vbCrLf & vbCrLf
' database and recordset object variables
[COLOR=DarkRed]Dim db As DAO.Database[/COLOR]
Dim rs As DAO.Recordset
' show message box and evaluate if the user has selected Yes or No
If MsgBox(Message1 & NL & Message2, vbQuestion + vbYesNo, Title) = vbYes Then
' open a connection to the current database
Set db = CurrentDb
Set rs = db.OpenRecordset("tblMakes")
' using the recordset object
With rs
.AddNew ' prepare to add a new record
.Fields("Make") = NewData ' add unfound data into field
.Update ' update the table
.Close ' close the recordset object
End With
Response = acDataErrAdded ' confirm record added
Else
Me.MyCombo.Undo ' clear the entry in the combobox
Response = acDataErrContinue ' confirm the record is not allowed
End If
Exit_ErrorHandler:
' de-initialise our object variables
Set rs = Nothing
Set db = Nothing
Exit Sub
Err_ErrorHandler:
' display error message and error number
MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
Resume Exit_ErrorHandler
End Sub