Full Version: ComboBox NotInList procedure
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
steIT
I have a comboBox (Make) on a form which has a Row source from another table listing laptop makes.
The user can add a new 'Make' which doesn't appear in the dropdown.
I am using NotInList event which updates the Make in the table, but the current record which is being created is saved and moves to a New record.

Is it possible to keep the current record after updating the new 'Make' in another table.

Private Sub combo78_NotInList(NewData As String, Response As Integer)
'Allow user to save non-list items.
Dim cnn As New ADODB.Connection
Dim strSQL As String
Dim bytUpdate As Byte

On Error GoTo ErrHandler

Set cnn = CurrentProject.Connection
bytUpdate = MsgBox("Do you want to add " & Me.Combo78.Text & " to the list?", vbYesNo, "Non-list item!")

If bytUpdate = vbYes Then

strSQL = "INSERT INTO tblMakeModel(Make) " & "VALUES ('" & NewData & "')"
Me!Combo78 = NewData
Debug.Print strSQL

cnn.Execute strSQL

Response = acDataErrAdded

ElseIf bytUpdate = vbNo Then

Response = acDataErrContinue

Me!Combo78.Undo

End If

Exit Sub

ErrHandler:

MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"

End Sub
Alan_G
Hi

Try

CODE
Private Sub combo78_NotInList(NewData As String, Response As Integer)
'Allow user to save non-list items.
On Error GoTo ErrHandler

Dim strSQL As String

If MsgBox("Do you want to add " & NewData & " to the list?", vbYesNo, "Non-list item!") = vbYes Then
    strSQL = "INSERT INTO tblMakeModel (Make) VALUES ('" & NewData & "')"

    Currentdb.Execute strSQL, dbFailOnError

    Response = acDataErrAdded

Else

    Response = acDataErrContinue

End If

ExitHere:
Exit Sub

ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
Resume ExitHere
End Sub
steIT
Thanks for quick reply, but that procedure seems to be doing the same. Moves to next "new" record after moving from combobox (data is added to table from combo78)

Steve
Alan_G
Hi Steve

I guess there's something else going on then, as there's nothing in there to move to any record - new or otherwise.

Do you have any code in any of the other events of the combo ?
steIT
No other events on Combo78.

Combo78 has control source from table 'Job sheet' as all other combo/textboxes.

Row Source for Combo78 is:

SELECT DISTINCT [tblMakeModel].ID, [tblMakeModel].Make FROM tblMakeModel ORDER BY [Make];

Steve
Alan_G
Easiest way to solve the mystery would be to take a look for you. Can you post a copy of your db (zipped to less than 500k and no sensitive data) and I'll have a look..........
steIT
Doh!

As I was stripping the database down to post, left the form with only a few combo boxes, tried adding new data and it worked ! Will try again on main form!

Thanks for you help Alan
steIT
Not sure if this was the problem, but combo78 started as a textBox and I changed it to a comboBox. When I added a combo box to form and used above code, all worked okay ?????

Steve
Alan_G
Very strange - but as long as it's working for you, as Homer would say, WhooHoo wink.gif
steIT
Still need help on this one. When adding a value to combo box which is not in the list, it asks if you would like to add to list, if yes, then the dropdown is initiates on the combobox but the value added is not in list. The value is however added to to the table referred to in row source.
jwhite
Try changing: Response = acDataErrorAdded
To: Response = acDataErrAdded
steIT
Thanks, that solves that problem. I am having the problem mentioned earler on this post were after selected yes to add to list, the form moves on to next 'new' record. I need to continue adding data to the current record.
jwhite
In the Form Properties, look for "Cycle".

If it is currently set to "All Records", change it to "Current Record".
steIT
Once again thank you for your help. This done the trick.

Steve thanks.gif
jwhite
You're Welcome! thumbup.gif Good luck with your project!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.