Ms access 使用基于查询的列表框
我试图从一个列表框中传递一个值,该列表框是从一个select查询创建的。我试图将名为“Hierarchy”的数值传递给我创建的名为tblHoldingGovernanceCommitteeID的表。我想在我的列表框中获取查询到的值,并将其带到表“tblHoldingGovernanceCommitteeID”中的字段“ID\u GovCommittee”。不幸的是,例程执行了,但没有向表传递任何值。我的代码如下:Ms access 使用基于查询的列表框,ms-access,vba,ms-access-2010,Ms Access,Vba,Ms Access 2010,我试图从一个列表框中传递一个值,该列表框是从一个select查询创建的。我试图将名为“Hierarchy”的数值传递给我创建的名为tblHoldingGovernanceCommitteeID的表。我想在我的列表框中获取查询到的值,并将其带到表“tblHoldingGovernanceCommitteeID”中的字段“ID\u GovCommittee”。不幸的是,例程执行了,但没有向表传递任何值。我的代码如下: Dim rst As DAO.Recordset Set rst = Curren
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblHoldingGovernanceCommitteeID", dbOpenDynaset)
rst.AddNew
rst!ID_GovCommittee = Hierarchy
rst.Update
rst.Close
Set rst = Nothing
为了帮助诊断/理解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
非常感谢韦恩,我很快会让你知道它是如何工作的。