Microsoft KB Archive/209878

= ACC2000: How to Move List Box Items to Another List Box =

Article ID: 209878

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q209878



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

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



SUMMARY
This article shows you how to create a form that has two list boxes that imitate the multiple-selection capability used in Microsoft Access wizards.



MORE INFORMATION
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. The method described in this article uses a Yes/No field in the table to indicate which records are selected. One list box displays the Yes records and the other displays the No records. To move items from one list box to the other, the Yes/No field of the selected record is set to the appropriate state and the list boxes are then requeried to update their respective lists.

In addition to the two list boxes, the form also has three command buttons. By using the command buttons, you can add items to, or delete items from, the list boxes. You can also double-click an item in a list box to move it to the other list box.

To create this example, follow these steps:

 Create a table that contains the data for the list boxes.   Open the sample database Northwind.mdb and create the following new table:   Table: Table1 --  Field Name: List_Item Data Type: Text

Field Name: Yes-No_Fld Data Type: Text

Table Properties: Table1 PrimaryKey: List_Item NOTE: The Text data type is not available in an Access project; use varchar instead.   View the Table1 table in Datasheet view. Add five records to the table. For each record, type the following sample data:   List_Item     Yes-No_Fld -    --   One           Yes Two          Yes Three        Yes Four         Yes Five         Yes   Create two queries based on the table that you created in step 1.   Create the following new query based on the Table1 table and save it as QueryYes: <pre class="fixed_text">  Query: QueryYes -  Field: List_Item Show: Yes Criteria: [Yes-No_Fld] = "Yes" </li>  Create another new query based on the Table1 table as follows and save it as QueryNo: <pre class="fixed_text">  Query: QueryNo --  Field: List_Item Show: Yes Criteria: [Yes-No_Fld] = "No" NOTE: In an Access project, create a view instead of a query. </li></ol> </li> Create a form containing list boxes, code, and command buttons.  Create a new blank form and save it as frmSelectList.</li>  Add the following list box and command button controls to the frmSelectList form: <pre class="fixed_text">  List Box: Name: ListYes RowSourceType: Table/Query RowSource: QueryYes OnDblClick: [Event Procedure]

List Box: Name: ListNo RowSourceType: Table/Query RowSource: QueryNo OnDblClick: [Event Procedure]

Command Button: -  Name: cmdClear Caption: Clear OnClick: [Event Procedure]

Command Button: -  Name: cmdAdd Caption: Add Item OnClick: [Event Procedure]

Command Button: -  Name: cmdDel Caption: Delete Item OnClick: [Event Procedure] NOTE: In an Access project, set the RowSourceType property of the list boxes to Table/View/StoredProc. </li>  With the frmSelectList form open in Design view, on the View menu, click Code, and then type the following line in the Declarations section of the Form module if it is not already there: Option Explicit </li>  Type the following five procedures in the Form module:NOTE: The sample code in this article uses Microsoft ActiveX Data Objects. For this code to run properly, you must reference the Microsoft ActiveX Data Objects 2.x Library (where 2.x is 2.1 or later.) To do so, click References on the Tools menu in the Visual Basic Editor, and make sure that the Microsoft ActiveX Data Objects 2.x Library check box is selected.

Private Sub cmdAdd_Click Dim conn As ADODB.Connection Dim MyRS As ADODB.Recordset Dim SelItem As Control ' set SelItem = to the selected item. Set SelItem = Me.ListYes If IsNull(SelItem) Then MsgBox "Please select an item from the list." Else ' set up the connection and recordset. Set conn = CurrentProject.Connection Set MyRS = New ADODB.Recordset ' open the recordset. MyRS.Open "Table1", conn, adOpenDynamic, adLockOptimistic With MyRS ' find the record for the selected item. .Find "List_Item = '" & SelItem & "'" ' set the Yes-No_Fld of the selected record to "No". .Fields("Yes-No_Fld").Value = "No" .Update End With Set MyRS = Nothing Set conn = Nothing ' make sure the list boxes have the current values. Me.ListYes.Requery Me.ListNo.Requery End If

End Sub Private Sub cmdClear_Click

Dim conn As ADODB.Connection Dim MyRS As ADODB.Recordset

' set up the connection and recordset. Set conn = CurrentProject.Connection Set MyRS = New ADODB.Recordset ' open the recordset. MyRS.Open "Table1", conn, adOpenDynamic, adLockOptimistic With MyRS ' loop through all records and set the Yes-No_Fld to "Yes". Do While Not .EOF .Fields("Yes-No_Fld").Value = "Yes" .Update .MoveNext Loop End With Set MyRS = Nothing Set conn = Nothing ' make sure the list boxes have the current values. Me.ListYes.Requery Me.ListNo.Requery

End Sub Private Sub cmdDel_Click

Dim conn As ADODB.Connection Dim MyRS As ADODB.Recordset Dim SelItem As Control ' set SelItem = to the selected item. Set SelItem = Me.ListNo If IsNull(SelItem) Then MsgBox "Please select an item from the list." Else ' set up the connection and recordset. Set conn = CurrentProject.Connection Set MyRS = New ADODB.Recordset ' open the recordset. MyRS.Open "Table1", conn, adOpenDynamic, adLockOptimistic With MyRS ' find the record for the selected item. .Find "List_Item = '" & SelItem & "'" ' set the Yes-No_Fld of the selected record to "Yes". .Fields("Yes-No_Fld").Value = "Yes" .Update End With Set MyRS = Nothing Set conn = Nothing ' make sure the list boxes have the current values. Me.ListNo.Requery Me.ListYes.Requery End If

End Sub Private Sub ListYes_DblClick(Cancel As Integer) ' run the code behind the command button cmdAdd cmdAdd_Click End Sub Private Sub ListNo_DblClick(Cancel As Integer) ' run the code behind the command button cmdDel cmdDel_Click End Sub </li> Save and close the Form module.</li> View the frmSelectList form in Form view.

Note that the first list box, ListYes, displays all the items in the Table1 table that you can select. You can use the Clear, Add Item, or Delete Item buttons to add or remove items from the second list box, ListNo. You can also double-click an item in a list box to move it to the other list box.</li></ol> </li></ol>

<div class="references_section">