Microsoft KB Archive/287493

= ACC2002: Access Quits Unexpectedly When You Set Listbox Recordset =

Article ID: 287493

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q287493



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SYMPTOMS
When you change the view of a form several times in succession, from Form view to Datasheet view to PivotTable view and then back to Form view, one of the following behaviors may occur:  Microsoft Access shuts down unexpectedly without returning any error message. Access shuts down after returning the following error error message:

Microsoft Access has encountered a problem and needs to close. We are sorry for the inconvenience.

The details of the error message indicate that Access caused an error in an unknown module.



CAUSE
This behavior can occur if you set the control's Recordset property after switching views. For example, you set the Recordset property after switching from Datasheet view to Form view.



RESOLUTION
To resolve this issue, use one of the following methods.

Set the RowSource Property of the Control
The RowSource property of the control allows you to specify the name of a table, query, or SQL statement to generate the list that the control will contain. To set the RowSource property, follow these steps:
 * 1) Open your form in Design view.
 * 2) On the View menu, click Properties to view the property sheet.
 * 3) Select the combo box or list box control to view its properties.
 * 4) On the Data tab in the property sheet, set the RowSourceType property to Table/Query.
 * 5) Set the RowSource property to the name of a table, query, or SQL statement.
 * 6) On the File menu, click Save.
 * 7) On the View menu, click Form View.

Note that the combo box or list box control contains a list of items from the table, query, or SQL statement that you specified in the RowSource property.
 * 1) Switch to other views of the form, and then back to Datasheet view or Form view.

Note that Access does not quit, and the combo box or list box control still contains items from the table, query, or SQL statement that you specified in the RowSource property.

Use the AddItem Method to Add Items to the Control
The AddItem method allows you to programmatically add values to the list portion of the control. This technique allows you to open a recordset and enumerate through it to add values to the combo box. To use the AddItem method to fill the control, follow these steps:

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. NOTE: The sample code in step 4 works only in an Access database (*.mdb).  Perform steps 1-5 of the &quot;Steps to Reproduce the Problem&quot; section that follows. Set the combo box's RowSourceType property to Value List. On the View menu, click Code to view the form's module.</li>  Add the following code to the form's module: Option Compare Database Option Explicit

Private Sub Form_Open(Cancel As Integer) Dim db As DAO.Database Dim rs As DAO.Recordset Dim strSQL As String strSQL = &quot;SELECT CategoryID, CategoryName FROM Categories &quot; & _ &quot;ORDER BY CategoryName&quot; Set db = CurrentDb Set rs = db.OpenRecordset(strSQL) Do Until rs.EOF Me.CategoryID.AddItem rs.Fields(&quot;CategoryID&quot;).Value & _ &quot;;&quot; & rs.Fields(&quot;CategoryName&quot;).Value rs.MoveNext Loop End Sub </li> Press ALT+Q to return to Access.</li> On the File menu, click Save.</li> On the View menu, click Form View.

Note that the Category control contains a list of items from the Categories table.</li> On the View menu, click Datasheet View.

Note that the Category control contains a list of items from the Categories table.</li> Switch to other views of the form and then back to Datasheet view or Form view.</li></ol>

Note that Access does not quit, and combo box contains items from the Categories table.

<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">

Steps to Reproduce the Problem
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

<ol> Open the sample database Northwind.mdb.</li> In the Database window, click Forms, click Products, and then click Design.</li> On the View menu, click Properties to display the property sheet.</li> Select the CategoryID combo box to view its properties.</li> On the Data tab of the property sheet, clear the RowSource property.</li> On the View menu, click Code to view the form's module.</li>  Add the following code to the form's module: Option Compare Database Option Explicit

Private db As DAO.Database Private rs As DAO.Recordset Private pFormView As Integer

Private Sub Form_Open(Cancel As Integer)

Dim strSQL As String strSQL = &quot;SELECT CategoryID, CategoryName FROM Categories &quot; & _ &quot;ORDER BY CategoryName&quot; Set db = CurrentDb Set rs = db.OpenRecordset(strSQL) Set Me.CategoryID.Recordset = rs   pFormView = Me.CurrentView End Sub

Private Sub Form_Current If Me.CurrentView <> pFormView Then Set Me.CategoryID.Recordset = rs       pFormView = Me.CurrentView End If

End Sub

Private Sub Form_ViewChange(ByVal Reason As Long) pFormView = Me.CurrentView End Sub </li> Press ALT+Q to return to Access.</li> On the File menu, click Save.</li> <li>On the View menu, click Form View.

Notice that the CategoryID combo box is filled with items from the Categories table, which indicates that the control is bound to a recordset.</li> <li>On the View menu, click Datasheet View.</li> <li>On the View menu, click PivotTable View.</li> <li>On the View menu, click Form View.</li> <li>On the View menu, click Design View.</li></ol>

Notice that Access quits unexpectedly with no error message.

<div class="references_section">