2017-04-19 63 views
0

我的表單上有兩個列表框。 對於在ConceptList中選擇的每個項目,我需要在ParkList中添加所有選擇的項目VBA在表格中插入兩個列表框值

附加的期望狀態圖像顯示了我在兩個選擇在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 

回答

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 
+0

啊..這就是它。感謝您的幫助 –