ACC03: I have an "F_Contacts" form which contains a listbox control listing types of services that person (record) provides. Some examples might be "Carpentry", "Drywalling", "Heavy Equipment Operator", etc.
When the user moves from one record to another, I would like to display their previous selections for the listbox.
The selections for each record are stored in a table "T_ContactServices" with three fields.
T_ContactServices
Field 1: ContactServicesID - autonumber index
Field 2: ContactID - long integer
Field 3: ServiceID - long integer
In the F_Contacts OnCurrent event, I call a sub that (is supposed to) pre-select the items in the listbox based on the T_ContactServices table.
Here's the code:
Private Sub P_ResetServices()
Dim rst As DAO.Recordset
Dim ctlList As Control
Dim varItem As Variant
Dim cnt As Integer
Dim F as Form
set F = Forms!F_Contacts
Set ctlList = F!lstServices
Set rst = CurrentDb.OpenRecordset("Select * from T_ContactServices")
rst.FindFirst ("ContactID=" & ContactID)
If rst.NoMatch Then ' No Services assigned to this Contact
For Each varItem In ctlList.ItemsSelected
ctlList.Selected(varItem) = False
Next varItem
Else ' Contact has Services selected
' Clear listbox
For Each varItem In ctlList.ItemsSelected
ctlList.Selected(varItem) = False
Next varItem
' Reset Services selected from T_ContactServices
Set rst = _
CurrentDb.OpenRecordset("Select * from T_ContactServices WHERE ContactID=" _
& ContactID)
rst.MoveLast
rst.MoveFirst
For cnt = 1 To rst.RecordCount
varItem = rst!ServiceID - 1
' The problem is here.
' rst!ServiceID is NOT the same as the ItemData index in the lstServices
' ex: Service = "Heavy Equipment Operator"
' ServiceID = "13"
' ItemData Index for "Heavy Equipment Operator" in lstServices = 6
' lstServices.ItemData(12) = "Roofing"
ctlList.Selected(varItem) = True
rst.MoveNext
Next cnt
End If
rst.Close
Set rst = Nothing
Set ctlList = Nothing
Set F = Nothing
End Sub
The problem is that I can't figure out how to get the proper ItemData index (ie. row) based on the ServiceID which is unfortunately NOT the same.
Maybe it's simple. I can't see it.
Any help greatly appreciated,
Penn
When the user moves from one record to another, I would like to display their previous selections for the listbox.
The selections for each record are stored in a table "T_ContactServices" with three fields.
T_ContactServices
Field 1: ContactServicesID - autonumber index
Field 2: ContactID - long integer
Field 3: ServiceID - long integer
In the F_Contacts OnCurrent event, I call a sub that (is supposed to) pre-select the items in the listbox based on the T_ContactServices table.
Here's the code:
Private Sub P_ResetServices()
Dim rst As DAO.Recordset
Dim ctlList As Control
Dim varItem As Variant
Dim cnt As Integer
Dim F as Form
set F = Forms!F_Contacts
Set ctlList = F!lstServices
Set rst = CurrentDb.OpenRecordset("Select * from T_ContactServices")
rst.FindFirst ("ContactID=" & ContactID)
If rst.NoMatch Then ' No Services assigned to this Contact
For Each varItem In ctlList.ItemsSelected
ctlList.Selected(varItem) = False
Next varItem
Else ' Contact has Services selected
' Clear listbox
For Each varItem In ctlList.ItemsSelected
ctlList.Selected(varItem) = False
Next varItem
' Reset Services selected from T_ContactServices
Set rst = _
CurrentDb.OpenRecordset("Select * from T_ContactServices WHERE ContactID=" _
& ContactID)
rst.MoveLast
rst.MoveFirst
For cnt = 1 To rst.RecordCount
varItem = rst!ServiceID - 1
' The problem is here.
' rst!ServiceID is NOT the same as the ItemData index in the lstServices
' ex: Service = "Heavy Equipment Operator"
' ServiceID = "13"
' ItemData Index for "Heavy Equipment Operator" in lstServices = 6
' lstServices.ItemData(12) = "Roofing"
ctlList.Selected(varItem) = True
rst.MoveNext
Next cnt
End If
rst.Close
Set rst = Nothing
Set ctlList = Nothing
Set F = Nothing
End Sub
The problem is that I can't figure out how to get the proper ItemData index (ie. row) based on the ServiceID which is unfortunately NOT the same.
Maybe it's simple. I can't see it.
Any help greatly appreciated,
Penn