VBA将两个列表框值插入表中

时间:2017-04-19 04:20:16

标签: loops ms-access access-vba do-while

我的表单上有两个列表框。 对于在ConceptList中选择的每个项目,我需要在ParkList

中添加所有选定的项目

附加的所需状态图像显示了在ConceptList中进行了两次选择而在ProspectList中进行了两次选择时的预期结果。当前状态显示以下代码的结果

Current State Image Desired State Image

  Dim db As Database
   Dim rs As Recordset

   Set db = CurrentDb()
   Set rs = db.OpenRecordset("tblFinal")
   For Each itm In ConceptList.ItemsSelected
        rs.AddNew
        rs!ConceptID = ConceptList.ItemData(itm)
        rs!ParkID = ParkList.ItemData(itm)
        rs.Update
  Next

  rs.Close
  Set rs = Nothing
  Set db = Nothing


End Sub

1 个答案:

答案 0 :(得分:0)

你还需要一个内循环:

Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb()
Set rs = db.OpenRecordset("tblFinal")

For Each concept In ConceptList.ItemsSelected
    For Each park In ParkList.ItemsSelected
        rs.AddNew
            rs!ConceptID = ConceptList.ItemData(concept)
            rs!ParkID = ParkList.ItemData(park)
        rs.Update
    Next
Next   
rs.Close

Set rs = Nothing
Set db = Nothing