Private Sub cboXCompany_NotInList(NewData As String, Response As Integer)
'Suppress the default error message.
Response = acDataErrContinue
' Prompt user to verify if they wish to add a new value.
If MsgBox("The company of " & NewData & " is not in list. Add it?", vbYesNo) = vbYes Then
' Set Response argument to indicate that data is being added.
'Open a recordset of the tblCompany Table.
Dim db As Database
Dim rstcboCompany As Recordset
Dim sqltblCompany As String
Set db = CurrentDb()
sqltblCompany = "Select * From tblCompany "
Set rstcboCompany = db.OpenRecordset(sqltblCompany, dbOpenDynaset)
'Add a new Company with the value that is stored in the variable NewData.
rstcboCompany.AddNew
rstcboCompany![CompanyName] = NewData
rstcboCompany.Update
'Inform the combo box that the desired item has been added to the list.
Response = acDataErrAdded
rstcboCompany.Close 'Close the recordset
End If
End Sub
|