Article ID: 177117
Article Last Modified on 1/22/2007
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.
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
List Selected
---- --------
one Yes
two Yes
three Yes
four Yes
five Yes
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![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
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()
Additional query words: combo fill inf multipleselection multiple-select multipleselect
Keywords: kbhowto kbprogramming KB177117