ACC2: Moving List Box Items from One List Box to AnotherID: Q132137
|
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.
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:
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
Query: Select Yes
-------------------------------
Field: List
Show: Yes
Criteria: [selected] = "YES"
Query: Select No
------------------------------
Field: List
Show: Yes
Criteria: [selected] = "NO"
Option Explicit
'=======================================================
' 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
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()
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