在 Access 中引用列表框

Referencing list boxes in Access

我是 Access 的新手,所以这可能是一个简单的问题。

我在 Access 2013 中有一个表单。有一个子表单显示来自 SQL 服务器的 table,就像这样。

Company   Product
-----------------
CompanyA  Product1
CompanyA  Product2
CompanyB  Product1
CompanyB  Product2

在列表框中使用 ListIndex,我可以显示我单击的任何行的索引。例如,如果我单击第二行 (CompanyA、Product2),列表框显示的 ListIndex 为 1。如果我单击第三行,则 ListIndex 为 2。

如何让列表框显示列的值而不是 ListIndex?

我想做的是,当我单击子表单中的一行时,我想在其自己的列表框中显示该行的每个列值。

但是,我似乎无法在更大的函数中将 ListIndex 用作变量。我尝试了以下操作:

我应该使用其他 属性 吗?我是否遗漏了我尝试过的属性?

要获取列表框的值,如果允许多select,请使用以下示例:

    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

术语似乎有点混乱。您的 List3 是一个列表框,而不是子表单。

字段 CompanyProduct 看起来像文本框,但是如果第一个字段有控件源 =[List3].[ListIndex],并且显示文本而不是数字,它似乎是一个高度为一行的列表框。

我建议对 CompanyProduct 使用 文本框 ,并使用这些控制源:

=[List3]

为绑定列。或者,为了保持一致性:=[List3].Column(0)

=[List3].Column(1)

第二列。

当您单击列表框中的项目时,这些文本框会自动更新。