2014-11-23 56 views
0

我想從選擇查詢創建的列表框中傳遞一個值。我試圖將名爲'Hierarchy'的數值傳遞給我創建的名爲tblHoldingGovernanceCommitteeID的表。我想在我的列表框中查詢所查詢的值,並將其帶到'tblHoldingGovernanceCommitteeID'表中的'ID_GovCommittee'字段。不幸的是,例程執行但沒有值傳遞給表。我的代碼如下:使用基於查詢的列表框

Dim rst As DAO.Recordset 
Set rst = CurrentDb.OpenRecordset("tblHoldingGovernanceCommitteeID", dbOpenDynaset) 
rst.AddNew 
rst!ID_GovCommittee = Hierarchy 
rst.Update 
rst.Close 
Set rst = Nothing 

回答

1

爲了幫助診斷/理解ListBox的工作方式,下面是一些您可以嘗試的代碼。只需更改名稱以匹配ListBox控件的名稱,然後選擇正確的列。查看發送到即時窗口的顯示。

Private Sub lstHierarchy_AfterUpdate() 
Dim strHierarchy As String 
Dim iRows   As Integer 
Dim iCols   As Integer 
Dim i    As Integer 
Dim strPrint  As String 

Dim varItem   As Variant 

    iRows = Me.lstHierarchy.ListCount  ' # rows (Items) in the listbox 
    iCols = Me.lstHierarchy.ColumnCount  ' # columns in the listbox 

    Debug.Print "Listbox has "; iRows & " rows; and " & iCols & " columns." 

    Debug.Print "Count of Items Selected (if Multi-Select allowed): " & Me.lstHierarchy.ItemsSelected.Count 


    For Each varItem In Me.lstHierarchy.ItemsSelected   ' Loop through the items selected (if multi-select allowed) 
     strPrint = "" 
     For i = 0 To iCols          ' Loop thru each column in a row 
      strPrint = strPrint & "|" & Me.lstHierarchy.Column(i, varItem) 
     Next i 
     Debug.Print "Selected Value: " & strPrint    ' Display the row; columns are delimited with '|' 
    Next 

    strHierarchy = Me.lstHierarchy.Column(0, varItem)  ' Change the Column from 0 to desired column. 

    ' This is your code (note I changed the name Hierarchy to strHierarchy) 
    Dim rst As DAO.recordSet 
    Set rst = CurrentDb.OpenRecordset("tblHoldingGovernanceCommitteeID", dbOpenDynaset) 
    rst.AddNew 
    rst!ID_GovCommittee = strHierarchy 
    rst.Update 
    rst.Close 
    Set rst = Nothing 

End Sub 
+0

非常感謝韋恩我會讓你知道它是如何工作的。 – 2014-11-23 16:41:14