View Full Version : Combo Box Question


Darren-Clift
04-10-2000, 08:51 AM
Im sort of new to this and wonder if someone can help me.How do you get a combo box to accept and save user data if not in the combo list.Thanks for your help

gino
04-10-2000, 09:46 AM
do not select limit to list on your properties and also select the field that you would want to store data to in the control source in your properties.

R. Hicks
04-10-2000, 04:07 PM
If you are using a table for the list of values for you combo box, add the following code in the Not In List event of your combo box:

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!YourCombo
' 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 products table.
NewData = StrConv(NewData, 3)
strSQL = " INSERT INTO tblYourTable ( YourField ) SELECT '" & NewData & "'"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
ctl.value = NewData
DoCmd.SetWarnings True
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:
If Err = 2113 Then
Err = 0
Resume Next
Else
MsgBox Str(Err)
MsgBox Err.Description
Resume Exit_YourCombo_NotInList
End If
End Sub

Note!! Change the name YourCombo in the code above to the name of your combo box. Also in the line that says:

strSQL = " INSERT INTO tblYourTable ( YourField ) SELECT '" & NewData & "'"

Change the name "tblYourTable" to the table you are using, and change "YourField" to the name of the field that contains your information.

Good Luck,
RDH