If you use a bound combo box, there are some additional issues that need to
be addressed. Here is the problem. When you enter a new value in a combo
box, you have changed the value in the bound field so now the record is
Dirty. Next, you try to add a new record using the new value, but before
that can happen, it updates the current record with the new value, so you
will get an error trying to add a duplicate record.
There are a couple of ways you can address this. One is to have your combo
as an unbound control and a hidden text box that is the bound control. Now
when you change the value in the combo, you are not affecting the bound value
and can add a new record without affecting the current record. The only
thing you need to do is update the hidden text box with the value of the
combo in the combo's After Update event.
The other way involves replacing the combo box's value with its OldValue
before trying to create a new record. Here is an example of that technique:
Private Sub cboActivity_NotInList(NewData As String, Response As Integer)
Dim rst As Recordset
If MsgBox(NewData & " Is Not In The Attribute Table " & vbNewLine _
& "Do you want to add it", _
vbInformation + vbYesNo, "Not Found") = vbYes Then
Me.cboActivity = Me.cboActivity.OldValue
CurrentDb.Execute ("INSERT INTO CISAttributeTable (ACTIVITY) " _
& "VALUES ('" & NewData & "');"), dbFailOnError
Me.Requery
Set rst = Me.RecordsetClone
rst.FindFirst "[Activity] = '" & NewData & "'"
Me.Bookmark = rst.Bookmark
Set rst = Nothing
Response = acDataErrAdded
Else
Me.cboActivity.Undo
Response = acDataErrContinue
End If
End Sub
As to the SQL problem, I think you might be missing one quote mark, but I am
not sure.
db.Execute "INSERT INTO tblTotalAgentListABCD (LastName) VALUES (""" &
NewData & """")", dbFailOnError
Gnowor said:
Klatuu said:
Since the combo box is not bound, it will now follow your record navigation.
Most likely, you have a text box on the form that is the same value you would
want yout combo box to show. If this is the case, you can set the Control
Source property for the combo to the text box:
=[MyTextBox]
(MyTextBox would be the name of the text box that is the same value you want
to show in the text box)
When I try to do this, it says I can't change the value because it's bound,
so the VBA code doesn't execute to find the right record.
Now, to add a new entery, you will want to use the Not In List event. This
site has a good explanation of this event and how to use it
http://www.pacificdb.com.au/MVP/Code/NIL.htm
From that site I got the following code and customized it for my
circumstances.
-----------
Private Sub AgentName_NotInList(NewData As String, Response As Integer)
Dim db As Database
Set db = CurrentDb
'Add the new value to the field
db.Execute "INSERT INTO tblTotalAgentListABCD (LastName) VALUES (""" &
NewData & """)", dbFailOnError
'Tell Access you've added the value
Response = acDataErrAdded
db.Close
Set db = Nothing
End Sub
-----------------------
And then it doesn't pick up the name of my table (table name is
TotalAgentListABCD, and field name is LastName) Thanks for the quick
response,
Klatuu.