ACC2002: Subform Combo Box Displays Incorrect Data in an Access Project (295252)
The information in this article applies to:
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 "Steps to Reproduce Behavior" 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("Products1").Form.Controls("CategoryID")
'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("Products1").Form.Controls("CategoryID")
'Store the current RowSource property and then clear it.
strRowSource = ctl.RowSource
ctl.RowSource = ""
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("Products1").Form.Controls("CategoryID")
'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.
- On the Records menu, click Data Entry. This clears both the main form and the subform so that no records are displayed.
- Begin typing a new category record into the main form, but do not save the record.
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.
STATUSMicrosoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. REFERENCESFor additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
295225 ACC2002: Write Conflict Error When You Add a Record in Multi-Table View
Modification Type: | Major | Last Reviewed: | 9/27/2003 |
---|
Keywords: | kbbug KbClientServer kbnofix KB295252 |
---|
|