<p>I have an Access 2007 form that is searchable by a combobox.  When I add a new record, I need to update the combobox to include the newly added item. </p>

<p>I assume that something needs to be done in AfterInsert event of the form but I can't figure out what.  </p>

<p>How can I rebind the combobox after inserting so that the new item appears in the list?</p>

[{'answer_id': 81147, 'author': 'Philippe Grondier', 'author_id': 11436, 'author_profile': 'https://Stackoverflow.com/users/11436', 'pm_score': 0, 'selected': False, 'text': '<p>I assume your combobox is a control on a form, not a combobox control in a commandBar. This combobox has a property called rowsource, that can be either a value list (husband;wife;son;girl) or a SQL SELECT instruction (SELECT relationDescription FROM Table_relationType). </p>\n\n<p>I assume also that your form recordset has something to do with your combobox recordset. What you\'ll have to do is, once your form recordset is <strong>properly updated</strong> (afterUpdate event I think), to reinitialise the rowsource property of the combobox control </p>\n\n<p>if the recordsource is an SQL instruction:</p>\n\n<pre><code>myComboBoxControl.recordsource = _\n    "SELECT relationDescription FROM Table_relationType"\n</code></pre>\n\n<p>or if it is a value list</p>\n\n<pre><code>myComboBoxControl.recordsource = myComboBoxControl.recordsource &amp; ";nephew"\n</code></pre>\n\n<p>But over all I find your request very strange. Do you have a reflexive (parent-child) relationship on your table? </p>\n'}, {'answer_id': 81153, 'author': 'Birger', 'author_id': 11485, 'author_profile': 'https://Stackoverflow.com/users/11485', 'pm_score': 2, 'selected': True, 'text': '<p>The easiest way is to guarantee that the combobox is always up-to-date is to just requery the combobox once it gets the focus. Even if the recordset is then updated somewhere else, your combobox is always up-to-date. A simple TheCombobox.Requery in the OnFocus event should be enough.</p>\n'}, {'answer_id': 81536, 'author': 'Fionnuala', 'author_id': 2548, 'author_profile': 'https://Stackoverflow.com/users/2548', 'pm_score': 0, 'selected': False, 'text': '<p>I would normally use the NotInList event to add data to a combo with  </p>\n\n<pre><code>   Response = acDataErrAdded\n</code></pre>\n\n<p>To update the combo.</p>\n\n<p>The Access 2007 Developers Reference has all the details, including sample code:\n<a href="http://msdn.microsoft.com/en-us/library/bb214329.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb214329.aspx</a></p>\n'}, {'answer_id': 81872, 'author': 'Chris OC', 'author_id': 11041, 'author_profile': 'https://Stackoverflow.com/users/11041', 'pm_score': 0, 'selected': False, 'text': "<p>Requery the combo box in the form's after update event and the delete event.  Your combo box will be up to date whenever the user makes changes to the recordset, whether it's a new record, and change, or a deletion.</p>\n\n<p>Unless users must have everybody else's changes as soon as they're made, don't requery the combo box every time it gets the focus because not only will the user have to wait (which is noticable with large recordsets), it's unnecessary if the recordset hasn't changed.  But if that's the case, the whole form needs to be requeried as soon as anybody else makes a change, not just the combo box.  This would be a highly unusual scenario.</p>\n\n<p>After update:</p>\n\n<pre><code>Private Sub Form_AfterUpdate()    \n    On Error GoTo Proc_Err \n\n    Me.cboSearch.Requery   \n\n    Exit Sub    \nProc_Err:    \n    MsgBox Err.Number &amp; vbCrLf &amp; vbCrLf &amp; Err.Description\n    Err.Clear        \nEnd Sub\n</code></pre>\n\n<p>After delete:</p>\n\n<pre><code>Private Sub Form_Delete(Cancel As Integer)    \n    On Error GoTo Proc_Err \n\n    Me.cboSearch.Requery   \n\n    Exit Sub    \nProc_Err:    \n    MsgBox Err.Number &amp; vbCrLf &amp; vbCrLf &amp; Err.Description\n    Err.Clear        \nEnd Sub\n</code></pre>\n"}, {'answer_id': 89106, 'author': 'David-W-Fenton', 'author_id': 9787, 'author_profile': 'https://Stackoverflow.com/users/9787', 'pm_score': 1, 'selected': False, 'text': '<p>There are two possible answers here that are efficient:</p>\n\n<ol>\n<li><p>use the Form\'s AfterInsert event to Requery the combo box (as well as the OnDeleteConfirm event). This will be sufficient if the combo box does not display data that the user can update and that needs to be updated if the underlying record is updated.</p></li>\n<li><p>if updates to the data need to be reflected in the combo box, then it would make sense to add a requery in the AfterUpdate events of the controls that are used to edit the data displayed in the combo box.</p></li>\n</ol>\n\n<p>For example, if your combo box lists the names of the people in the table, you\'ll want to use method #2, and in the AfterUpdate event of Me!txtFirstName and Me!txtLastName, requery the combo box. Since you\'re doing the same operation in four places, you\'ll want to write a subroutine to do the requery. So, the sub would look something like this:</p>\n\n<pre><code>  Private Sub RequerySearchCombo()\n    If Me.Dirty Then Me.Dirty = False\n    Me!MyCombo.Requery\n  End Sub\n</code></pre>\n\n<p>The reason to make sure you requery only when there is actually an update to the data displayed in the combo box is because if you\'re populating the combo box with the list of the whole table, the requery can take a very long time if you have 10s of 1,000s of records.</p>\n\n<p>Another alternative that saves all the requeries would be to have a blank rowsource for the combo box, and populate it only after 1 or 2 characters have been typed, and filter the results that the combo displays based on the typed characters. For that, you\'d use the combo box\'s OnChange event:</p>\n\n<pre><code>Private Sub MyCombo_Change()\n  Dim strSQL As String\n\n  If Len(Me!MyCombo.Text) = 2 Then\n     strSQL = "SELECT MyID, LastName &amp; \', \' &amp; FirstName FROM MyTable "\n     strSQL = strSQL &amp; "WHERE LastName LIKE " &amp; Chr(34) &amp; Me!MyCombo.Text &amp; Chr(34) &amp; "*"\n     Me!MyCombo.Rowsource = strSQL \n  End If\nEnd Sub\n</code></pre>\n\n<p>The code above assumes that you\'re searching for a person\'s name in a combo box that displays "LastName, FirstName".</p>\n\n<p>There\'s another important caveat: if you\'re searching a form bound to a full table (or a SQL statement that returns all the records in the table) and using Bookmark navigation to locate the records, this method will not scale very well, as it requires pulling the entire index for the searched fields across the wire. In the case of my imaginary combo box above, you\'d be using FindFirst to navigate to the record with the corresponding MyID value, so it\'s the index for MyID that would have to be pulled (though only as many index pages as necessary to satisfy the search would actually get pulled). This is not an issue for tables with a few thousand records, but beyond about 15-20K, it can be a network bottleneck.</p>\n\n<p>In that case, instead of navigating via bookmark, you\'d use your combo box to filter the result set down to the single record. This is, of course, extremely efficient, regardless of whether you\'re using a Jet back end or a server back end. It\'s highly desirable to start incorporating these kinds of efficiencies into your application as soon as possible. If you do so, it makes it much easier to upsize to a server back end, or makes it pretty painless if you should hit that tipping point with a mass of new data that makes the old method too inefficient to be user-friendly.</p>\n'}]
['https://Stackoverflow.com/questions/80832', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14579/']