我有一个Access2017数据库。在表单上,我有“ PLCS组”多选列表框,有“ IPS元素”第二个多选列表框。
-----FIRST LIST-----
ID
GRAPES
APPLES
CHERRIES
LETTUCE
--------------------
-----SECOND LIST----
FK
COLD
DARK
LEAVE IN SUN
DIED
---------------------
So if someone selected GRAPES and APPLES from first list. And selected COLD,DIED from second list
This is what the second table should have it it.
GRAPES,COLD
GRAPES,DIED
APPLES,COLD
APPLES,DIED
我尝试嵌套第二个列表框的记录,但是并没有更新表。 我将代码更改为建议的代码,但是现在出现错误3020“没有AddNew或Edit的更新或CancelUpdate”
我有AddNew语句,我不确定是什么使它无法识别它们。如果我注释掉第二个循环,则会保存第一个列表框项目。 这是我的更新代码:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim ctl As Control
Dim ctl2 As Control
Dim varItem As Variant
Dim varItem2 As Variant
Set db = CurrentDb()
Set rs = db.OpenRecordset("ProposalTracker3", dbOpenDynaset, dbAppendOnly)
Set rs2 = db.OpenRecordset("IPSElementsDetails", dbOpenDynaset, dbAppendOnly)
Set ctl = Me.PLCSGroupListBox
Set ctl2 = Me.IPSList
For Each varItem In ctl.ItemsSelected
rs.AddNew
rs!GACP = ctl.ItemData(varItem)
rs.Update
For Each varItem2 In ctl2.ItemsSelected
rs2.AddNew
rs2!ProposalID = Me.txtID
rs2!Element = ctl2.ItemData(varItem2)
rs.Update
Next varItem2
rs2.Close
Next varItem
rs.Close
db.Close
答案 0 :(得分:1)
似乎您希望两个列表框中所选项目的所有可能组合。您将条目与两个嵌套循环组合在一起
Set db = CurrentDb()
Set rs = db.OpenRecordset("DestinationTable", dbOpenDynaset, dbAppendOnly)
Set lb1 = Me.PLCSGroupListBox
Set lb2 = Me.IPSList
For Each item1 In lb1.ItemsSelected
For Each item2 In lb2.ItemsSelected
rs.AddNew
rs!Somefield = lb1.ItemData(item1) & ',' & lb2.ItemData(item2)
rs!ProposalID = Me.ID
rs.Update
Next item2
Next item1
rs.Close
db.Close
或者您可能想将两个列表框中的值添加到两个单独的字段中
rs.AddNew
rs!Somefield = lb1.ItemData(item1)
rs!Anotherfield = lb2.ItemData(item2)
...