Add to a Listbox

This example will add an item to a list box. It uses the Northwinds and Solutions databases that come as samples for Access.

'***************** Code Start *******************
'From EnterOrEditProducts Form on Solutions Database

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
   Dim 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
     ' Open AddCategory form.
     DoCmd.OpenForm "AddCategory", acNormal, , , acAdd, acDialog, NewData
     ' Continue without displaying default error message.
     Response = acDataErrAdded
   End If
End Sub

'****************** Code End ********************

Return to Example List