I have a many to many relationship database using 3 tables:
units - tab1(junction table) - addons
A unit can have many addons and each addon can be related to many units.
Now, on the form I have 3 listboxes:
list1: shows all the units. Selecting a unit form list1 sets the form current record, since the form itself is bound to the the unit table.
list2: shows the single unit's addons, based on list1 selected unit.
list3: shows all the addons
I wish to add some others addons to the selected unit on double click on list3 (the addons list) and consequentially requery the list2. I'm using the following code:
Private Sub list3_DblClick(Cancel As Integer)
Dim ctrl As Control
Dim strSQL As String
Dim varItem As Variant
Set ctrl = Me.list3
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSQL = "INSERT INTO tab1(addID) " & _
"SELECT addons.addID " & _
"FROM addons " & _
"WHERE addons.addID = " & ctrl.ItemData(varItem)
CurrentDb.Execute strSQL
Next varItem
End If
Me.list2.Requery
End Sub
It works partially, since the new addon is added to the junction table, but the connection field "unitID" is blank, and the event has no effect on list2.
My question is: how can I also fill the unitID value, based on the list1 selected unit (which is also the form current record) and requery the list2?
Besides, is it possible to prevent access inserting the same addon twice? I tried the WHERE NOT EXISTS method, but I really don't know the proper syntax...
Please, even a little advice on how approach this problem would be appreciated.
I'm also posting the database.
Many thanks to the forum people.
units - tab1(junction table) - addons
A unit can have many addons and each addon can be related to many units.
Now, on the form I have 3 listboxes:
list1: shows all the units. Selecting a unit form list1 sets the form current record, since the form itself is bound to the the unit table.
list2: shows the single unit's addons, based on list1 selected unit.
list3: shows all the addons
I wish to add some others addons to the selected unit on double click on list3 (the addons list) and consequentially requery the list2. I'm using the following code:
Private Sub list3_DblClick(Cancel As Integer)
Dim ctrl As Control
Dim strSQL As String
Dim varItem As Variant
Set ctrl = Me.list3
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strSQL = "INSERT INTO tab1(addID) " & _
"SELECT addons.addID " & _
"FROM addons " & _
"WHERE addons.addID = " & ctrl.ItemData(varItem)
CurrentDb.Execute strSQL
Next varItem
End If
Me.list2.Requery
End Sub
It works partially, since the new addon is added to the junction table, but the connection field "unitID" is blank, and the event has no effect on list2.
My question is: how can I also fill the unitID value, based on the list1 selected unit (which is also the form current record) and requery the list2?
Besides, is it possible to prevent access inserting the same addon twice? I tried the WHERE NOT EXISTS method, but I really don't know the proper syntax...
Please, even a little advice on how approach this problem would be appreciated.
I'm also posting the database.
Many thanks to the forum people.