Microsoft KB Archive/295252

= ACC2002: Subform Combo Box Displays Incorrect Data in an Access Project =

Article ID: 295252

Article Last Modified on 9/27/2003

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q295252



Moderate: Requires basic macro, coding, and interoperability skills.

This article applies only to a Microsoft Access project (.adp).



SYMPTOMS
When you insert a new record into a main form that contains a linked subform that has a combo box, the value shown in the text portion of the combo box is incorrect.



CAUSE
The subform is linked to the main form by means of an Identity field, and the main form has been filtered so that only a subset of the records is showing.



RESOLUTION
There are two possible resolutions for this problem.

Remove the Identity Field from the Main Form
The easiest solution is to remove the Identity field, or the field that links the forms, from the main form. You can still use the Identity field to link the main form and the subform in the link properties of the subform. However, the combo box in the subform will display the correct value if the Identity field is not present on the main form.

Programmatically Assign the Row Source of the Combo Box
A second solution is to bind and unbind the RowSource property of the combo box. Because the problem occurs only during the insertion of a new record, you can clear the RowSource property of the combo box during the BeforeInsert event, and then bind it back during the AfterUpdate and Undo events. For example, you can do this by following these steps:  First, follow the steps in the &quot;Steps to Reproduce Behavior&quot; section later in this article. Then, open the main form in Design view. On the View menu, click Code to view the module of the form.  Add the following code to the module: Option Compare Database Option Explicit

Dim strRowSource As String

Private Sub Form_AfterUpdate Dim ctl As Access.Control

'Reference the combo box control on the subform. Be sure 'to change this code to refer to the proper control on your 'subform. Set ctl = Me.Controls(&quot;Products1&quot;).Form.Controls(&quot;CategoryID&quot;)

'Assign the RowSource property back to the string we stored 'during the BeforeInsert event. ctl.RowSource = strRowSource End Sub

Private Sub Form_BeforeInsert(Cancel As Integer) Dim ctl As Access.Control

'Reference the combo box control on the subform. Be sure 'to change this code to refer to the proper control on your 'subform. Set ctl = Me.Controls(&quot;Products1&quot;).Form.Controls(&quot;CategoryID&quot;)

'Store the current RowSource property and then clear it. strRowSource = ctl.RowSource ctl.RowSource = &quot;&quot; End Sub

Private Sub Form_Undo(Cancel As Integer) Dim ctl As Access.Control

'Reference the combo box control on the subform. Be sure 'to change this code to refer to the proper control on your 'subform. Set ctl = Me.Controls(&quot;Products1&quot;).Form.Controls(&quot;CategoryID&quot;)

'Assign the RowSource property back to the string we stored 'during the BeforeInsert event. ctl.RowSource = strRowSource End Sub  On the File menu, click Close and Return to Microsoft Access. On the View menu, click Form View. Note that the subform displays product records that correspond to the current category record on the main form.</li> On the Records menu, click Data Entry. This clears both the main form and the subform so that no records are displayed.</li> Begin typing a new category record into the main form, but do not save the record.</li></ol>

Note that the CategoryID field on the main form contains the value 1, even though this value has already been used. After saving the record on the main form, note that the Identity field is populated with its assigned value from the server, and the text portion of the combo box in the subform correctly displays data.

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

MORE INFORMATION
When you insert a record into a table that contains an Identity field, Microsoft Access automatically populates the Identity field with a value one greater than the maximum identity value of the records that the form has retrieved.

For example, assume that the main form has been filtered so that the maximum value in the Identity field is 10. When you insert a new record into the main form, Microsoft Access automatically populates the Identity field with the next highest value, 11, even though this value may already have been used. When the record on the main form is saved, this value is discarded in place of the actual Identity value assigned by the server. However, before the record is saved, any control referencing the Identity field will display the same incorrect value.

Steps to Reproduce the Behavior
<ol> CAUTION: If you follow the steps in this example, you modify the sample Access project NorthwindCS.adp. You may want to back up the NorthwindCS.adp file and follow these steps on a copy of the project.

</li> Open the sample project NorthwindCS.adp.</li> Close the Main Switchboard form if it appears.</li> On the View menu, point to Database Objects, and then click Forms.</li> Click New.</li> In the New Form dialog box, click Form Wizard, and then click OK.</li> In the Tables/Queries drop-down box, click the Products table.</li> Add the ProductID, ProductName, and CategoryID fields to the Selected Fields list, and then click Next.</li> Click Datasheet for the form layout, and then click Finish. Note that a new form named Products1 is created and opens in Datasheet view.</li> On the View menu, click Design View to view the design of the new form.</li> Click the CategoryID text box, on the Format menu, point to Change To, and then click Combo Box.</li>  Set the properties of the combo box as follows: <pre class="fixed_text">  Combo box -  Name: CategoryID ControlSource: CategoryID RowSource: Categories ColumnCount: 2 ColumnWidths: 0&quot;; 1&quot; BoundColumn: 1 List Width: 1&quot; </li> On the View menu, click Datasheet View.</li> <li>Click the CategoryID column, and then on the Format menu, click Column Width.</li> <li>Click Best Fit. This automatically resizes the CategoryID column so that all data is visible.</li> <li>On the File menu, click Save, and then close the form.</li> <li>Open the Categories form in Design view.</li> <li>On the View menu, click Field List to display the field list of the form.</li> <li>Drag the CategoryID field from the field list to the detail section of the form.</li> <li>Click the Product List subform, and then delete it.</li> <li>From the Database window, drag the Products1 form into the detail section of the form to create a new subform.</li> <li> Set the properties of the subform as follows: <pre class="fixed_text">  Subform Name: Products1 LinkChildFields: CategoryID LinkMasterFields: CategoryID Top: 1.5&quot; Width: 5.5&quot; </li> <li>On the View menu, click Form View. Note that the subform displays product records that correspond to the current category record on the main form.</li> <li>On the Records menu, click Data Entry. This clears both the main form and the subform so that no records are displayed.</li> <li>Begin typing a new category record into the main form.</li></ol>

Note that the text portion of the combo box in the subform displays the value Beverages, which is the first category in the Categories table.

<div class="references_section">