VBA:如何从组合框中选择项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20103964/
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 17:15:42  来源:igfitidea点击:

VBA: How to select item from a ComboBox

vbams-accesscomboboxaccess-vbams-access-2010

提问by ksagar

I have Access 2010 form which has a ComboBox cmbSubTopicwhich lists two columns (SubTopicIDand SubTopic). The combo box is bound to a field containing SubTopicID. The SubTopicIDcolumn in the combo box is hidden, it only shows the SubTopic. When the user selects a SubTopicfrom the drop down the corresponding SubTopicIDis stored in the table. I wrote some VBA code for the on load event of the form to look up the SubTopicIDin the table and the corresponding SubTopicis selected in the ComboBox. My current code is something like this:

我有 Access 2010 表单,它有一个 ComboBox cmbSubTopic,其中列出了两列(SubTopicIDSubTopic)。组合框绑定到包含 的字段SubTopicIDSubTopicID组合框中的列是隐藏的,它只显示SubTopic. 当用户SubTopic从下拉列表中选择一个时,相应的SubTopicID将存储在表中。我为表单的加载事件编写了一些VBA代码,以便在表格中查找SubTopicIDSubTopic在ComboBox中选择相应的。我目前的代码是这样的:

Set rsST = dbs.OpenRecordset(strSqlst)
For i = 0 To Me.cmbSubTopic.ListCount - 1
    If Me.cmbSubTopic.Column(0, i) = rsST.Fields("SubTopicID").Value Then
        Me.cmbSubTopic.SetFocus
        Me.cmbSubTopic.Selected(i) = True
        Exit For
    End If
Next i

This gives the error saying:

这给出了错误说:

The text you entered isn't an item in the list

您输入的文本不是列表中的项目

I also tried using this:

我也尝试使用这个:

Me.cmbSubTopic = Me.cmbSubTopic.Selected(i)

This selects the item in the ComboBox but it also writes the value of I in to the IDfield of the table which I don't want.

这会选择 ComboBox 中的项目,但它也会将 I 的值写入ID我不想要的表字段。

回答by HansUp

Assuming the combo's first column, SubTopicID, is also the combo's "bound column" property, it is used as the column's .Valueproperty. That means you only need to assign a value to .Valuein order to select the matching combo row.

假设组合的第一列 ,SubTopicID也是组合的“绑定列”属性,它将用作列的.Value属性。这意味着您只需为其分配一个值.Value即可选择匹配的组合行。

Me.cmbSubTopic.Value =  rsST.Fields("SubTopicID").Value

That approach is simple, but I'm uncertain whether it is the appropriate solution for your situation. We don't know anything about your rsSTrecordset --- I presumed the SubTopicIDfield in the recordset's current row is the value you want selected in the combo. If I misunderstood that point, we need to figure out something different.

这种方法很简单,但我不确定它是否适合您的情况。我们对您的rsST记录集一无所知--- 我假设SubTopicID记录集当前行中的字段是您要在组合中选择的值。如果我误解了这一点,我们需要找出不同的东西。

If the combo is bound to a field in the form's record source, this suggestion would also change the stored value. If you don't want that, "unbind" the combo --- in other words, make its Control Sourceproperty blank.

如果组合绑定到表单记录源中的字段,此建议也会更改存储的值。如果您不希望那样,请“解除绑定”组合 --- 换句话说,将其Control Source属性设置为空白。

回答by Vlado

Here are 2 subs - one for passing text (SubTopic) and the second for ID (SubTopicId):

这里有 2 个子 - 一个用于传递文本(SubTopic),第二个用于 ID(SubTopicId):

Public Sub SelectComboBoxItemByText(cmb As ComboBox, Value As String)
    On Error GoTo ErrHandler_
Dim i As Integer
    For i = 0 To cmb.ListCount - 1
        If cmb.Column(1, i) = Value Then
            cmb.SetFocus
            cmb.Selected(i) = True
            cmb.Text = Value
            Exit For
        End If
    Next i
End Sub
ExitProc_:
    DoCmd.Hourglass False
    Exit Sub
ErrHandler_:
    DoCmd.Hourglass False
    Call LogError(Err, "basTools", "SelectComboBoxItemByText")
    Resume ExitProc_
    Resume ' use for debugging
End Sub

Public Sub SelectComboBoxItemById(cmb As ComboBox, Value As Integer)
    On Error GoTo ErrHandler_
Dim i As Integer
    For i = 0 To cmb.ListCount - 1
        If cmb.Column(0, i) = Value Then
            cmb.SetFocus
            cmb.Selected(i) = True
            cmb = Value
            Exit For
        End If
    Next i
ExitProc_:
    DoCmd.Hourglass False
    Exit Sub
ErrHandler_:
    DoCmd.Hourglass False
    Call LogError(Err, "basTools", "SelectComboBoxItemById")
    Resume ExitProc_
    Resume ' use for debugging
End Sub