ACC2: Moving List Box Items from One List Box to Another

ID: Q132137


The information in this article applies to:
  • Microsoft Access 2.0


SUMMARY

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

This method 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.

Note that the method provided 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. See the "References" section later in this article for more information on a multiuser technique.


MORE INFORMATION

In addition to the two list boxes, the form also contains 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 form, follow these steps:

  1. Open the sample database NWIND.MDB and create the following new table:
    
          Table: Table1
          ---------------------------------------------------
          Field Name: List
             Data Type: Text
             Field Size: 15
             Caption: Items that will be provided in list
          Field Name: Selected
             Data Type: Text
             Field Size: 5
             Caption: Indicates if the item has been selected
    
          Table Properties: Table1
          ------------------------
          PrimaryKey: List 


  2. View the Table1 table in Datasheet view. Add five records to the table. For each record, type any text in the List field (for example, a list of colors or cities), and then type YES in the Selected field.


  3. Create the following new query based on the Table1 table and save it as Select Yes:
    
          Query: Select Yes
          -------------------------------
          Field: List
             Show: Yes
             Criteria: [selected] = "YES" 


  4. Create another new query based on the Table1 table as follows and save it as Select No:
    
          Query: Select No
          ------------------------------
           Field: List
             Show: Yes
             Criteria: [selected] = "NO" 


  5. Create a new blank form and save it as SelectList.


  6. With the SelectList form open in Design view, click Code on the View menu, and then type the following line in the form module's Declarations section:
    
           Option Explicit 


  7. 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![Field0]
    
              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
    
                 MyTable.Edit
                 MyTable.[Selected] = "no"
                 MyTable.Update
    
                 MyTable.Close
                 Me![Field0].Requery
                 Me![Field2].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![Field2]
    
              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
    
                 MyTable.Edit
                 MyTable.[Selected] = "yes"
                 MyTable.Update
    
                 MyTable.Close
                 Me![Field0].Requery
                 Me![Field2].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
    
              MyTable.MoveFirst
              Do Until MyTable.EOF
                MyTable.Edit
                MyTable.[Selected] = "yes"
                 MyTable.Update
                  MyTable.MoveNext
              Loop
    
              MyTable.Close
              Me![Field0].Requery
              Me![Field2].Requery
    
              erhandle:
                 Resume Next
    
           End Function 


  8. Save and close the form module. Then, add the following list box and command button controls to the SelectList form:
    
          List Box:
          -----------------------------
          Name: Field0
             RowSourceType: Table/Query
             RowSource: Select Yes
             OnDblClick: =Add()
    
          List Box:
          -----------------------------
          Name: Field2
             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() 


  9. View the SelectList form in Form view. Note that the first list box, Field0, displays all the items in Table1 you can select. You can use the Clear, Add Item, or Delete Item buttons to add or remove items from the second list box, Field2. You can also double-click an item in a list box to move it to the other list box.



REFERENCES

For more information about how to select more than one item from the same list box (using an array), see the following article in the Microsoft Knowledge Base:

Q121356 ACC2: How to Create a Multiple-Selection list box

Additional query words: combo fill

Keywords : kbusage FmsCmbo
Version : 2.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: April 29, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.