tags:

views:

183

answers:

2

When I select the value from the combo box, related value should appear in the text box

ComboBox code.

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

In comboBox list of ID is displaying, when I select the particular person id, Name should display in text box related to the personid

Text Box

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

I put the above code in Form_Load Event, Nothing displaying in Text Box.

What wrong in my code.

Need VB6 code Help

+1  A: 

You would want the 2nd block of code in the the click event for the combobox.

Edit

There looks like another couple of issues in your code at this line:

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

2 Issues:

  • You are passing in the control itself as the person ID, not the selected value.
  • You have an extra space in your query after the person ID
  • You should change that line to be:

    cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
    
    James
    This reply is correct the code needs to be in the Click event not the form load event.
    RS Conley
    +1  A: 

    Why not have the combobox display the name and hold the personID as it's item data?

    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
    

    Then, if you need the PersonID for the selected name you can just grab combo.ItemData(combo.ListIndex).

    C-Pound Guru