Use the notinlist event of the combo box. This is described in "Access
Developer's Handbook" (Litwin, Getz, Gilbert). Following are two examples.
The first opens a form to allow a new part number to be added. The second
example just fills in information without opening a form.
Sample One:
Private Sub cboPartNo_NotInList(NewData As String, Response As Integer)
On Error GoTo Err_Label
Dim strMessage As String
Dim strFormName As String
strMessage = "Part Number: '" & UCase$(NewData) & "' is not in the
list. " _
& vbCrLf & "Would you like to add it?"
If MsgBox(strMessage, vbYesNo + vbQuestion, "Add New Part") = vbYes
Then
strFormName = "frmTool_PartAddShort"
DoCmd.OpenForm strFormName, acNormal, , , acFormAdd, acDialog,
UCase$(NewData)
Response = acDataErrAdded
Else
Response = acDataErrDisplay
End If
Exit_Label:
Exit Sub
Err_Label:
MsgBox Err.Description
Resume Exit_Label
End Sub
Sample Two:
Private Sub Country_NotInList(NewData As String, Response As Integer)
On Error GoTo Err_Label
Dim strMessage As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
strMessage = "'" & UCase$(NewData) & "' is not in the list. " _
& vbCrLf & "Would you like to add it?"
If MsgBox(strMessage, vbYesNo + vbQuestion, "New Country") = vbNo Then
Response = acDataErrDisplay
Else
Set db = CurrentDb()
Set rst = db.OpenRecordset("tlkCountry")
With rst
.AddNew
.Fields("Country") = UCase$(NewData)
.Update
End With
Response = acDataErrAdded
End If
Exit_Label:
On Error Resume Next
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
Exit Sub
Err_Label:
MsgBox Err.Description
Resume Exit_Label
End Sub