ACC: How to Move List Box Items to Another List Box (7.0/97)
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 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.
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 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 box. You can also double-click an item in a list box to
move it to the other list box.
To create the list boxes with the multiple-selection capability, you need to do the following:
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
- 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
- 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
- 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"
- 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
- Create a new blank form and save it as SelectList.
- 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
- 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
- Save and close the Form module.
- 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()
- 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
Issue type : kbhowto