ACC: How to Move List Box Items to Another List Box (7.0/97)ID: Q177117
|
Moderate: Requires basic macro, coding, and interoperability skills.
This article shows you how to 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.
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 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
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()
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
Issue type : kbhowto
Last Reviewed: July 6, 1999