OK, here’s the as-advertised example of using the NotInList event to dynamically add an item to a combo-box row source. The same principles apply to list boxes.
Two sections of relevant code: The first is the NotInList event of the combo box. It posts a replacement to the default message, and if the message response is “yes”, opens the add-item form in dialog mode, so that the NotInList code is paused while the add-item form is open. The NewData parameter is loaded by VBA with the value that was keyed in (and not found), so I save that in a Public-level variable that is read into the add-item form. The value “acDataErrAdded” designates that a new value was added to the RowSource, so the combo is automagically re-queried once the add-item form closes.
Private Sub cboConductor_NotInList(NewData As String, Response As Integer)
'enable addition of new items to conductor list
Dim intAnswer As Integer, strMessage As String
strMessage = "This item is not in the list." & vbNewLine & "Do you want to add it?"
intAnswer = MsgBox(strMessage, vbYesNo + vbQuestion, "Not Found")
If intAnswer = vbYes Then 'push the new entries into public variables
glNewItem = NewData
glCategory = 3
'open New Item form
DoCmd.OpenForm "pfrmAddNewLookupItem", , , , , acDialog
'requery the combo box
Response = acDataErrAdded
Else 'ignore the new item
Response = acDataErrContinue
End If
End Sub
Here now is the code for events in the add-item form – First, the Form_Load event pulls in the NewData information saved in the Public variable and drops it in the proper control on the form: no need to re-type what was originally input into the combo box. Then, the “Add” button on the form checks that all required input is present and posts the new item to the source table before closing the form and returning to the combo’s NotInList event code:
Private Sub cmdAdd_Click()
'add new item to lookup table
Dim myset As DAO.Recordset
Dim strSQL As String, strMessage As String
strMessage = "Please enter a new item!"
'check for user entry
If txtDescription = "" Then MsgBox strMessage, vbOKOnly + vbExclamation, "Input Needed"
Exit Sub
End If
strMessage = "The " & Chr(34) & "new" & Chr(34) & " item already exists." & vbNewLine
strMessage = strMessage & "Please re-check your entry and try again."
'define and open recordsets
strSQL = "SELECT * FROM tblLookupItems;"
Set myset = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
'add the new item to the recordset
'check that the new entry does not already exist
With myset .FindFirst "CategoryID = " & txtCategoryID & " and Description = " & Chr(34) & txtDescription & Chr(34)
If .NoMatch = True Then
.AddNew
!CategoryID = txtCategoryID
!Description = txtDescription
.Update
Else
MsgBox strMessage, vbOKOnly + vbCritical, "Error"
GoTo CA_cleanup
End If
End With
'close and release recordsets
CA_cleanup:
myset.Close
Set myset = Nothing
'close the form
DoCmd.Close
End Sub
Obviously, you'll want to replace the control- and table-names used here with the ones you actually use, but the concept will be the same.
Steve