ACC2000: How to Move List Box Items to Another List Box
ID: Q209878
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
This article applies to a Microsoft Access database (.mdb) and 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 INFORMATIONMicrosoft 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 professionals 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 a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/
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/overview/overview.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 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:
Query: QueryYes
---------------------------------
Field: List_Item
Show: Yes
Criteria: [Yes-No_Fld] = "Yes"
- Create another new query based on the Table1 table as follows and save it as QueryNo:
Query: QueryNo
------------------------------
Field: List_Item
Show: Yes
Criteria: [Yes-No_Fld] = "No" NOTE: In an Access project, create a view instead of a query.
- Create a form containing list boxes, code, and command buttons.
- Create a new blank form and save it as frmSelectList.
- Add the following list box and command button controls to the frmSelectList form:
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.
- 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
- 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 need to reference the Microsoft ActiveX Data Objects 2.1 Library.
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
- Save and close the Form module.
- 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.
REFERENCESFor more information about list boxes, click Microsoft Access Help on the
Help menu, type list boxes: what they are and how they work in the Office Assistant or
the Answer Wizard, and then click Search to view the topic.
Additional query words:
combo fill inf multipleselection multiple-select multipleselect
Keywords : kbdta AccCon
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
|