【问题标题】:When I select the value from the combo box, related value should appear in the text box当我从组合框中选择值时,相关值应出现在文本框中
【发布时间】:2009-09-28 07:10:55
【问题描述】:

当我从组合框中选择值时,相关值应该出现在文本框中

组合框代码。

cmd.CommandText = "select distinct PERSONID from T_PERSON"
    Set rs = cmd.Execute
    While Not rs.EOF
        If Not IsNull(rs("PersonID")) Then
            txtno.AddItem rs("PersonID")
        End If
        rs.MoveNext
    Wend

在显示 ID 的组合框列表中,当我选择特定的人员 id 时,名称应显示在与 personid 相关的文本框中

文本框

cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
    Set rs = cmd.Execute

    While Not rs.EOF
       If Not IsNull(rs("Name")) Then
    txtName.Text = rs("Name")
           rs.MoveNext
        End If
    Wend

我把上面的代码放在 Form_Load 事件中,文本框中没有显示任何内容。

我的代码有什么问题。

需要VB6代码帮助

【问题讨论】:

    标签: vb6


    【解决方案1】:

    您可能希望组合框的单击事件中的第二个代码块。

    编辑

    这行代码中似乎还有另外几个问题:

    cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
    

    2 个问题:

  • 您将控件本身作为人员 ID 传递,而不是选定的值。
  • 您在查询中的人员 ID 后面有一个额外的空格
  • 您应该将该行更改为:

    cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
    

    【讨论】:

    • 这个回复是正确的,代码需要在 Click 事件而不是表单加载事件中。
    【解决方案2】:

    为什么不让组合框显示名称并将 personID 保存为项目数据?

    cmd.CommandText = "select distinct PERSONID, Name from T_PERSON WHERE PersonID IS NOT NULL"
    Set rs = cmd.Execute
    While Not rs.EOF
         combo.AddItem rs("Name").value
         combo.ItemData(combo.NewIndex) = rs("PERSONID").value
         rs.MoveNext
    Wend
    

    然后,如果您需要所选名称的 PersonID,您可以获取 combo.ItemData(combo.ListIndex)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-17
      • 2013-03-28
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 2016-12-06
      相关资源
      最近更新 更多