This will do it.....
In the not in list event.... Change "Customer(s)" to reflect your own Table
and Field names.
Private Sub MyComboBox_NotInList(NewData As String, Response As Integer)
Beep
MyComboBox = AddToList(NewData, "Customers", "Customer", True)
Response = acDataErrContinue
End Sub
this function can be called from any ComboBox. Place it in a Separate Module
in the Modules Window.
Public Function AddToList(ByVal NewData As String, ByVal TableName As
String, ByVal FieldName As String, Optional PromptUser As Boolean) As Long
On Error GoTo errTrap
Dim rs As New ADODB.Recordset
Select Case PromptUser
Case True
If MsgBox("Add this data to the list?", vbYesNo, "Not In List") = vbNo Then
Screen.ActiveControl.Text = ""
Exit Function
End If
Case False
'Do Nothing
End Select
Dim SQL As String
SQL = "Select * From [" & TableName & "]"
rs.Open SQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
With rs
..AddNew
..Fields(FieldName) = StrConv(NewData, vbProperCase)
AddToList = !CustomerID
..Update
Screen.ActiveControl = AddToList
Screen.ActiveControl.Requery
End With
Exit Function
err_Exit:
If rs.State = 1 Then rs.Close
Set rs = Nothing
errTrap:
Resume err_Exit
'Error Handling Code Goes Here
End Function
'/////////////////
B Comrie
http://www.codewidgets.com
Jess said:
I have construted a form to edit records in a table. I use an unbound combo
box to navigate through the records. The combo box LimitToList is set to
Yes. If the value entered is not in the list, I want to be able to add the
record to the table. I have tried the NotInList Event. I cannot figure out
the code to use to add a new record to the table. Please help