Option Compare Database
Option Explicit'Form Module to make a to from list
'Developed by: MajP
'Last Update:17 May 08
'
'Purpose: This Form Module takes two listboxes and four command buttons and allows the user to move items
'back and forth between the list boxes. This works with multi select list boxes and row source queries
'This is probably a little more efficient than the class module because it does not really on
'converting the row source to a value list. However, this requires the user to create a table
'and ensure that the listboxes have the primary keys bound.
'
''You database must have the following Table:
' tblSelected
' tblSelected must have the the following fields:
' numFK - of number type
' strFK - of text type
'
'Your form must have the following Controls:
' lstOne - the listbox on the left to select from
' lstTwo - the listbox on the right to move to
' cmdOne - > move highlighted values from lstOne to lstTwo
' cmdTwo - >> move all values from lstOne to lstTwo
' cmdThre - < move highlighted values from lstTwo to lstOne
' cmdFour - << move all values from lstTwo to lstOne
'
'
'lstOne and lstTwo should have the same rowsource and format except for the following:
'In the query for lstOne, in the criteria for the primary key field place the following:
' If a numeric primary key: "Not In (select numFK from tblSelected)"
' If a text field primary key: "Not In (select strFK from tblSelected)"
'
'In the query for lstTwo, in the criteria for the primary key field need the following:
' If a numeric primary key: " IN (select numFK from tblSelected)"
' If a text field primary key: " IN (select strFK from tblSelected)"
'
'example for lstOne:
'"SELECT tblScouts.ScoutID, tblScouts.strLastName FROM tblScouts
' WHERE tblScouts.autoScoutID
' Not In (select numFK from tblSelected)"
'
'example for lstTwo:
'"SELECT tblScouts.ScoutID, tblScouts.strLastName FROM tblScouts
' WHERE tblScouts.autoScoutID
' IN (select numFK from tblSelected)"
Private Sub cmdFour_Click()
Call removeAllFromSelected
End Sub
Private Sub cmdOne_Click()
Call addToSelected
End Sub
Private Sub cmdThree_Click()
Call removeFromSelected
End Sub
Private Sub cmdTwo_Click()
addAllToSelected
End Sub
Private Sub Form_Load()
Dim strSql As String
strSql = "Delete * from tblSelected"
DoCmd.SetWarnings (False)
DoCmd.RunSQL strSql
DoCmd.SetWarnings (True)
listRefresh
End Sub
Public Sub removeFromSelected()
'Ensure that primary key is the bound value of the listbox
Dim varItem As Variant
Dim varData As Variant
For Each varItem In Me.lstTwo.ItemsSelected
varData = Me.lstTwo.ItemData(varItem)
Call deleteData(varData, getPrimaryKeyType)
Next varItem
Call listRefresh
End Sub
Public Sub addToSelected()
'Ensure that primary key is the bound value of the listbox
Dim varItem As Variant
Dim varData As Variant
For Each varItem In Me.lstOne.ItemsSelected
varData = Me.lstOne.ItemData(varItem)
Call insertData(varData, getPrimaryKeyType)
Next varItem
Call listRefresh
End Sub
Public Function getPrimaryKeyType() As String
Dim rs As DAO.Recordset
Set rs = Me.lstOne.Recordset
Select Case rs.Fields(0).Type
Case 10
getPrimaryKeyType = "Text"
Case 4, 16, 9, 20, 7, 15
getPrimaryKeyType = "Number"
Case Else
MsgBox "Error with primary key. Check that primary key is bound field of listbox"
End Select
End Function
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 addAllToSelected()
Dim lstItem As Integer
For lstItem = 0 To lstOne.ListCount - 1
lstOne.Selected(lstItem) = True
Next lstItem
addToSelected
End Sub
Public Sub removeAllFromSelected()
Dim lstItem As Integer
For lstItem = 0 To lstTwo.ListCount - 1
lstTwo.Selected(lstItem) = True
Next lstItem
removeFromSelected
End Sub
Public Sub listRefresh()
lstOne.RowSource = lstOne.RowSource
lstTwo.RowSource = lstTwo.RowSource
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