This is probably a better way that I use. It provides a lot of flexibility.
1)make a table "tblSelected"
tblSelected
selectedID (PK, autonumber)
numFK (FK, long if the table uses a numeric PK)
strFK (FK, text if the table uses a text PK)
This table is where you store a list of the selected items. So if you have employees with numeric employee IDs then you would store the selected employee ID in the numFK field. If the employee ID is text then store the selected emp ID in the strFK.
This can be modified, but I wrote the code like this to reuse often.
2) So if you select an item you add its key to the tblSelected, if you unselect the person you remove them from tbl selected.
Code:
Private Sub Form_Load()
call ClearSelected
End Sub
Public Sub clearSelected()
Dim strSql As String
strSql = "Delete * from tblSelected"
DoCmd.SetWarnings (False)
DoCmd.RunSQL strSql
DoCmd.SetWarnings (True)
End Sub
Public Sub insertData(varData As Variant, primaryKeyType As String)
Dim strSql As String
If primaryKeyType = "Text" Then
strSql = "insert into tblSelected (strFK) values('" & varData & "')"
Else
strSql = "insert into tblSelected (numFK) values(" & varData & ")"
End If
DoCmd.SetWarnings (False)
DoCmd.RunSQL strSql
DoCmd.SetWarnings (True)
End Sub
Public Sub deleteData(varData As Variant, primaryKeyType As String)
Dim strSql As String
If primaryKeyType = "Text" Then
strSql = "delete * from tblSelected where strFK = '" & varData & "'"
Else
strSql = "delete * from tblSelected where numFK = " & varData
End If
DoCmd.SetWarnings (False)
DoCmd.RunSQL strSql
DoCmd.SetWarnings (True)
End Sub
So then my queries become something like
"Unselected people"
SELECT
tblScouts.autoScoutID,
tblScouts.strFirstName,
tblScouts.strLastName
FROM
tblScouts
WHERE
(((tblScouts.autoScoutID) Not In (select numFK from tblSelected)));
"Selected People"
SELECT
tblScouts.autoScoutID,
tblScouts.strFirstName,
tblScouts.strLastName
FROM
tblScouts
WHERE
(((tblScouts.autoScoutID) IN (select numFK from tblSelected)));
The above code and table is generic and can be very easily reused.
The above demo uses this concept to build a To-->From listbox like you would have in a wizard allowing you to move items between lists.