I have a form with several combo boxes and would like to be able to add a record to the underlying table when the selection I want is not in the list. Does anyone have an idea on how to write the code behind the NotInList Event?
The following is from Microsoft's 'Solutions' sample database in Acc97 - it should get you started:
Private Sub CategoryID_NotInList(NewData As String, Response As Integer)
' Add a new category by typing a name in CategoryID combo box.
Dim intNewCategory As Integer, intTruncateName As Integer, strTitle As String, intMsgDialog As Integer
' Display message box asking if user wants to add a new category.
strTitle = "Category Not In List"
intMsgDialog = vbYesNo + vbQuestion + vbDefaultButton1
intNewCategory = MsgBox("Do you want to add a new category?", intMsgDialog, strTitle)
If intNewCategory = vbYes Then
' Remove new name from CategoryID combo box so
' control can be requeried when user returns to form.
DoCmd.RunCommand acCmdUndo
' Display message box and adjust length of value entered in
' CategoryID combo box.
strTitle = "Name Too Long"
intMsgDialog = vbOKOnly + vbExclamation
If Len(NewData) > 15 Then
intTruncateName = MsgBox("Category names can be no longer than " _
& "15 characters. The name you entered will be truncated.", _
intMsgDialog, strTitle)
NewData = Left(NewData, 15)
End If
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.