G
Guest
I posted this last week and didn't receive a reply.
I am using John Vinson's sample code (the one about the animal shelter) but
I'm
having some issues applying it to my form. I have a table of taxi companies.
I have a subtable of cities they serve (a junction table). I want the feel
of a multiselect listbox for the user to select the cities the taxi serve.
(instead of having a combo box where they have to manually selecta record for
each city). So after they selected a few cities, they click on a button and
it will add new cities to the junction table, delete any cities that weren't
selected and keep any existing ones.
I was able to get the code to work but I'm having problems displaying what
was selected when a user goes to a taxi company record. So, when a user goes
from one taxi company record to the next, I want the listbox to be updated
with the cities they served highlighted. I assume the code will have to be
in the Oncurrent function of the form. But I'm not sure how to code it.
Below is the modified code to add/delete cities:
Dim iItem As Integer
Dim lngCondition As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
' save the current record if it's not saved
If Me.Dirty = True Then
Me.Dirty = False
End If
Set db = CurrentDb
' Open a Recordset based on the table
Set rs = db.OpenRecordset("tblAreaServe", dbOpenDynaset, dbSeeChanges)
With Me!lstCity
' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
lngCondition = .Column(0, iItem)
' Determine whether this City is currently in the table
rs.FindFirst "[OrganizationID] = " & Me.OrganizationID & " AND " _
& "[CityID] = " & lngCondition
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.addnew
rs!OrganizationID = Me.OrganizationID
rs!CityID = lngCondition
rs.Update
End If ' if it wasn't selected, ignore it
Else
If Not .Selected(iItem) Then
' delete this record if it's been deselected
rs.Delete
End If ' if it was selected, leave it alone
End If
Next iItem
End With
rs.close
Set rs = Nothing
Set db = Nothing
Thanks.
Ngan
I am using John Vinson's sample code (the one about the animal shelter) but
I'm
having some issues applying it to my form. I have a table of taxi companies.
I have a subtable of cities they serve (a junction table). I want the feel
of a multiselect listbox for the user to select the cities the taxi serve.
(instead of having a combo box where they have to manually selecta record for
each city). So after they selected a few cities, they click on a button and
it will add new cities to the junction table, delete any cities that weren't
selected and keep any existing ones.
I was able to get the code to work but I'm having problems displaying what
was selected when a user goes to a taxi company record. So, when a user goes
from one taxi company record to the next, I want the listbox to be updated
with the cities they served highlighted. I assume the code will have to be
in the Oncurrent function of the form. But I'm not sure how to code it.
Below is the modified code to add/delete cities:
Dim iItem As Integer
Dim lngCondition As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset
' save the current record if it's not saved
If Me.Dirty = True Then
Me.Dirty = False
End If
Set db = CurrentDb
' Open a Recordset based on the table
Set rs = db.OpenRecordset("tblAreaServe", dbOpenDynaset, dbSeeChanges)
With Me!lstCity
' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
lngCondition = .Column(0, iItem)
' Determine whether this City is currently in the table
rs.FindFirst "[OrganizationID] = " & Me.OrganizationID & " AND " _
& "[CityID] = " & lngCondition
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.addnew
rs!OrganizationID = Me.OrganizationID
rs!CityID = lngCondition
rs.Update
End If ' if it wasn't selected, ignore it
Else
If Not .Selected(iItem) Then
' delete this record if it's been deselected
rs.Delete
End If ' if it was selected, leave it alone
End If
Next iItem
End With
rs.close
Set rs = Nothing
Set db = Nothing
Thanks.
Ngan