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

Last reviewed: January 5, 1998
Article ID: Q177117
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

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

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 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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 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. The following general steps describe the process that is necessary for creating the list boxes with the multiple- selection capability. Each of these steps is explained in detail later in this article.

   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

   1. 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

   2. 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

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

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

C. Create a form containing list boxes, code, and command buttons

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

   2. 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

   3. 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

   4. Save and close the Form module.

   5. Add the following list box and command button controls to the
      SelectList form:

         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()

   6. 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.

REFERENCES

For more information about list boxes, search the Help Index for "List Boxes," or ask the Microsoft Access 97 Office Assistant.


Additional query words: combo fill inf multipleselection multiple-select
multipleselect
Keywords : FmsCmbo
Version : WINDOWS:7.0,97
Platform : WINDOWS
Hardware : x86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.