ACC2002: Access Quits Unexpectedly When You Set Listbox Recordset (287493)
The information in this article applies to:
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:
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:
- Open your form in Design view.
- On the View menu, click Properties to view the property sheet.
- Select the combo box or list box control to view its properties.
- On the Data tab in the property sheet, set the RowSourceType property to Table/Query.
- Set the RowSource property to the name of a table, query, or SQL statement.
- On the File menu, click Save.
- 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. - 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 "Steps to Reproduce the Problem" 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.
- 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 = "SELECT CategoryID, CategoryName FROM Categories " & _
"ORDER BY CategoryName"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
Do Until rs.EOF
Me.CategoryID.AddItem rs.Fields("CategoryID").Value & _
";" & rs.Fields("CategoryName").Value
rs.MoveNext
Loop
End Sub
- Press ALT+Q to return to Access.
- On the File menu, click Save.
- On the View menu, click Form View.
Note that the Category control contains a list of items from the Categories table. - On the View menu, click Datasheet View.
Note that the Category control contains a list of items from the Categories table. - Switch to other views of the form and then back to Datasheet view or Form view.
Note that Access does not quit, and combo box contains items from the Categories table.
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:
282358 ACC2002: Combo Box or List Box Recordset Is Lost When You Switch Form Views
Modification Type: | Major | Last Reviewed: | 6/23/2005 |
---|
Keywords: | kbbug kberrmsg kbnofix KB287493 |
---|
|