Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox - Allow a user to add values to the underlying Value List

Forms

Combobox - Allow a user to add values to the underlying Value List

by  rhicks  Posted    (Edited  )
Have you ever set the Row Source Type of your ComboBox to a Value List then needed to add the the list in runtime? The following code will use the Not In List event of the ComboBox to give the user a choice to add new item to Value List.

In the example below "YourCombo" needs to be the actual name of your combobox.

Private Sub YourCombo_NotInList(NewData As String, Response As Integer)
On Error GoTo Err_YourCombo_NotInList

Dim ctl As Control
Dim strSQL As String

' Return Control object that points to combo box.
Set ctl = Me!Status
' Prompt user to verify they wish to add new value.
If MsgBox("Item is not in list. Add it?", vbOKCancel) = vbOK Then
' Set Response argument to indicate that data is being added.
Response = acDataErrAdded
' Add string in NewData argument to value list
Status.RowSource = Status.RowSource & ";" & NewData
ctl.value = NewData
Else
' If user chooses Cancel, suppress error message and undo changes.
Response = acDataErrContinue
ctl.Undo
End If

Exit_YourCombo_NotInList:
Exit Sub

Err_YourCombo_NotInList:
MsgBox Err.Description
Resume Exit_YourCombo_NotInList

End Sub

HTH
RDH
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top