从组合框选择项目 [英] Select item from a ComboBox

查看:111
本文介绍了从组合框选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

这会导致错误:


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

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

我也尝试过使用此命令:

I also tried using this:

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

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

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

推荐答案

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

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

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

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

That approach is simple, but I'm uncertain whether it is the appropriate solution for your situation. We don't know anything about your rsST recordset --- I presumed the SubTopicID field 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.

如果该组合绑定到表单的记录源中的某个字段,则此建议还将更改存储的内容。值。如果您不希望这样做,请取消绑定组合---换句话说,将其 Control Source 属性设置为空白。

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 Source property blank.

这篇关于从组合框选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆