访问 VBA:根据非绑定列在组合框中查找项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15366805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:07:41  来源:igfitidea点击:

Access VBA: Find item in combo box based on non-bound column

vbams-accessaccess-vbams-access-2010

提问by Kai

I have a two-column combo box on an Access form representing a key-to-code mapping. The first column of the combo box is the 'bound column' (ie, the column used when MyComboBox.Valueis called).

我在 Access 表单上有一个两列组合框,表示键到代码的映射。组合框的第一列是“绑定列”(即MyComboBox.Value调用时使用的列)。

I need to dynamically set the Valueof my combo box based on a value found in the second column. For eg, if my combo box source is:

我需要Value根据在第二列中找到的值动态设置组合框的 。例如,如果我的组合框源是:

Value | Code
===============
 A1    | ABCD
 A2    | EFGH
 A3    | IJKL

I can set the value of the combo box simply with ComboBox.Value = "A2", but how would I do the same using the second column? ComboBox.Value = "EFGH"obviously isn't valid. Essentially looking for logic along the lines of ComboBox.Value = ComboBox.ValueWhereSecondColumnEquals("EFGH")

我可以简单地用 设置组合框的值ComboBox.Value = "A2",但是我如何使用第二列来做同样的事情?ComboBox.Value = "EFGH"显然是无效的。基本上沿着以下路线寻找逻辑ComboBox.Value = ComboBox.ValueWhereSecondColumnEquals("EFGH")

回答by RichardC

And assuming it's not based on a table/query:

并假设它不是基于表/查询:

Dim i As Integer

For i = 0 To ComboBox.ListCount-1
    If ComboBox.Column(1, i) = "EFGH" Then
        ComboBox.Value = ComboBox.ItemData(i)
        Exit For
    End If
Next i

回答by Fionnuala

Assuming that your combo is based on a table, you can DLookUp the value in the table:

假设您的组合基于一个表,您可以 DLookUp 表中的值:

 ComboBox.Value = Dlookup("Value","Table","Code='" & sCode & "'")

回答by David Locke

If the source is the value selected in a combobox, and the target is an unbound combobox set the .BoundColumn property of the target combobox to the appropriate column and then just assign the combobox values normally, as in cboX=cboY. Even if the target combobox is bound, you can dynamically change the bound column as needed.

如果源是在组合框中选择的值,而目标是未绑定的组合框,则将目标组合框的 .BoundColumn 属性设置为适当的列,然后正常分配组合框值,如 cboX=cboY。即使绑定了目标组合框,您也可以根据需要动态更改绑定列。