Microsoft KB Archive/177117

= ACC: How to Move List Box Items to Another List Box (7.0/97) =

Article ID: 177117

Article Last Modified on 1/22/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q177117



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



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

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: The method provided in this article is suitable for single-user environments only. If this method is used in a multiuser environment, what one user does may interfere with the actions of another user.



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 box. You can also double-click an item in a list box to move it to the other list box.

To create the list boxes with the multiple-selection capability, you need to do the following:   A. Create a table that contains the data for the list boxes.   B. Create two queries based on the table created in step A.    C. Create the form that will contain the list boxes, code modules, and command buttons.

A. 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 Data Type: Text Caption: Items that will be provided in list

Field Name: Selected Data Type: Text Caption: Indicates if the item has been selected

Table Properties: Table1 PrimaryKey: List   View the Table1 table in Datasheet view. Add five records to the table. For each record, type the following sample data:         List         Selected one          Yes two          Yes three        Yes four         Yes five         Yes 

B. Create two queries based on the table created in step A
  Create the following new query based on the Table1 table and save it as Select Yes: <pre class="fixed_text">        Query: Select Yes ---        Field: List Show: Yes Criteria: [selected] = "YES" </li>  Create another new query based on the Table1 table as follows and save it as Select No: <pre class="fixed_text">     Query: Select No      -- Field: List Show: Yes Criteria: [selected] = "NO" </li></ol>

C. Create a form containing list boxes, code, and command buttons
 Create a new blank form and save it as SelectList.</li>  With the SelectList form open in Design view, on the View menu, click Code, and then type the following line in the Form module Declarations section if it is not already there: Option Explicit </li>  Type the following three functions in the Form module: '=======================================================      ' The following function opens the table and changes the ' selected value from YES to NO, and then runs the ' query for the two list boxes so that they will display ' the updated values. '=======================================================      Function Add Dim MyDB As Database Dim MyTable As Recordset Dim y As Control

Set y = Me![list0]

If IsNull(y) Then MsgBox "Please select something in the list." Else Set MyDB = DBEngine.Workspaces(0).Databases(0) Set MyTable = MyDB.OpenRecordset("Table1")

MyTable.Index = "PrimaryKey" MyTable.Seek "=", y

With MyTable .Edit !Selected = "no" .Update End With

Set MyTable = Nothing Me![list0].Requery Me![list2].Requery End If      End Function

'=======================================================      ' The following function opens the table and changes the ' selected value from NO to YES, and then runs the ' query for the two list boxes so that they will display ' the updated values. '=======================================================

Function Del Dim MyDB As Database Dim MyTable As Recordset Dim y As Control

Set y = Me![list2]

If IsNull(y) Then MsgBox "Please select something in the list." Else Set MyDB = DBEngine.Workspaces(0).Databases(0) Set MyTable = MyDB.OpenRecordset("Table1")

MyTable.Index = "PrimaryKey" MyTable.Seek "=", y

With MyTable .Edit !Selected = "yes" .Update End With

Set MyTable = Nothing Me![list0].Requery Me![list2].Requery End If      End Function

'=======================================================      ' The following function sets all values in the Selected ' field to YES, and then runs the query for the two list ' boxes so that they will display the updated values. '=======================================================      Function Clear Dim MyDB As Database Dim MyTable As Recordset

Set MyDB = DBEngine.Workspaces(0).Databases(0) Set MyTable = MyDB.OpenRecordset("Table1")

On Error GoTo erhandle With MyTable .MoveFirst Do Until .EOF .Edit !Selected = "yes" .Update .MoveNext Loop

End With Set MyTable = Nothing Me![list0].Requery Me![list2].Requery

erhandle: Resume Next

End Function </li> Save and close the Form module.</li>  Add the following list box and command button controls to the SelectList form: <pre class="fixed_text">        List Box: -        Name: List0 RowSourceType: Table/Query RowSource: Select Yes OnDblClick: =Add

List Box: -        Name: List2 RowSourceType: Table/Query RowSource: Select No           OnDblClick: =Del

Command Button: Name: Button One Caption: Clear OnClick: =Clear

Command Button: Name: Button Two Caption: Add Item OnClick: =Add

Command Button: ---        Name: Button Three Caption: Delete Item OnClick: =Del </li> View the SelectList form in Form view.

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

<div class="references_section">