从办公室访问中的另一个组合框中的所选项目中选择组合框中的项目 [英] select item in combobox from selected item in another combobox in office access

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

问题描述

大家好,

我在访问数据库文件中形成,这种形式有两个组合框,这两个组合框从同一个表中绑定数据。

如何编写代码为了让用户在第一个组合框中选择项目时,第二个组合框会根据从组合框中选择的项目的索引自动选择一个项目?



thanx

解决方案

尝试这样的事情:

 私人  Sub  ComboBox1_Change()
Dim sSQL AS 字符串

sSQL = SELECT FieldName1& vbcr& _
FROM TableName& vbcr& _
WHERE FieldName1 =& Me .ComboBox1.Value

Me .ComboBox2.RowSource = sSQL
结束 Sub


谢谢Maciej Los,

我改进了你的答案:

当选择ItemId组合框时,NCode组合框会根据ItemId值选择正确的项目,反之亦然。

 私人  Sub  ItemId_Change()
Dim sSQL As String
sSQL = SELECT Items.NationalCode,Items.ItemId& FROM Items& WHERE Items.ItemId =& Me .ItemId.Value
Me .NCode.RowSource = sSQL
Me .NCode = Me .NCode.ItemData( 0
结束 Sub

私有 Sub ItemId_GotFocus()
Dim sSQL 作为 字符串
sSQL = SELECT Items.GenericName,Items.ItemId& FROM Items
Me .ItemId.RowSource = sSQL
结束 Sub

< span class =code-keyword>私有 Sub NCode_Change()
Dim sSQL 作为 字符串
sSQL = SELECT Items.GenericName,Items.ItemId& FROM Items& WHERE Items.ItemId =& Me .NCode.Value
Me .ItemId.RowSource = sSQL
Me .ItemId = Me .ItemId.ItemData( 0
结束 Sub

私有 Sub NCode_GotFocus()
Dim sSQL 作为 字符串
sSQL = SELECT Items.NationalCode,Items.ItemId& FROM Items
Me .NCode.RowSource = sSQL
结束 Sub


Hi guys,
I form in access database file, in this form have two combobox, these two combobox bind data from same table.
How can I write code in order to make user when select item from the first combobox the second combobox will automatically select an item based on the index of the item selected from combobox?

thanx

解决方案

Try something like this:

Private Sub ComboBox1_Change()
    Dim sSQL AS String

    sSQL = "SELECT FieldName1" & vbcr & _
        "FROM TableName" & vbcr & _
        "WHERE FieldName1 = " & Me.ComboBox1.Value

        Me.ComboBox2.RowSource = sSQL
End Sub


Thank you Maciej Los,
I improved your answer to this:
when choose ItemId combobox, NCode combobox will choose the correct item according to ItemId value, and vice versa.

Private Sub ItemId_Change()
Dim sSQL As String
    sSQL = "SELECT Items.NationalCode, Items.ItemId " & "FROM Items" & " WHERE Items.ItemId = " & Me.ItemId.Value
        Me.NCode.RowSource = sSQL
        Me.NCode = Me.NCode.ItemData(0)
End Sub

Private Sub ItemId_GotFocus()
Dim sSQL As String
    sSQL = "SELECT Items.GenericName, Items.ItemId" & " FROM Items"
        Me.ItemId.RowSource = sSQL
End Sub

Private Sub NCode_Change()
Dim sSQL As String
    sSQL = "SELECT Items.GenericName, Items.ItemId" & " FROM Items" & " WHERE Items.ItemId = " & Me.NCode.Value
        Me.ItemId.RowSource = sSQL
        Me.ItemId = Me.ItemId.ItemData(0)
End Sub

Private Sub NCode_GotFocus()
Dim sSQL As String
    sSQL = "SELECT Items.NationalCode, Items.ItemId" & " FROM Items"
        Me.NCode.RowSource = sSQL
End Sub


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

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