Results 1 to 4 of 4

Thread: [RESOLVED] Fill combobox depending on value of other combo

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Resolved [RESOLVED] Fill combobox depending on value of other combo

    Hello,

    I have 2 comboboxes.
    When I choose a country in cboLand I want only the states to show in cboStaat that are associated with the country from cboLand.

    I tried the code below but I get all different error messages.
    Perhaps you can see something in my code that I missed.

    Code:
    Public Sub SelectLand()
        Dim adapter As New OleDbDataAdapter("SELECT Land_ID,Land FROM tblLAND", connection)
        Dim table As New DataTable
        adapter.Fill(table)
    
        With Me.cboLand
          .DataSource = table
          .DisplayMember = "Land"
          .ValueMember = "Land_ID"
          .SelectedIndex = 0
        End With
      End Sub
    
        Public Sub SelectStaat()
            Dim adapter As New OleDbDataAdapter("SELECT Staat_ID,Staat FROM tblSTAAT WHERE Land_ID = " & Me.cboLand.SelectedItem, connection)
            Dim table As New DataTable
            adapter.Fill(table)
    
    
            With Me.cboStaat
                .DataSource = table
                .DisplayMember = "Staat"
                .ValueMember = "Staat_ID"
                .SelectedIndex = 0
            End With
        End Sub
    Thanks in advance

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Fill combobox depending on value of other combo

    On which line is the error?
    I think in the select statement the "Me.cboLand.SelectedItem" is wrong. it should be "CInt(Me.cboLand.SelectedValue)" if Land_ID type is integer.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: Fill combobox depending on value of other combo

    Quote Originally Posted by VBDT View Post
    On which line is the error?
    I think in the select statement the "Me.cboLand.SelectedItem" is wrong. it should be "CInt(Me.cboLand.SelectedValue)" if Land_ID type is integer.
    Thanks for the quick reply.

    When I use CInt(Me.cboLand.SelectedValue), the error is on line
    Dim adapter As New OleDbDataAdapter("SELECT Staat_ID,Staat FROM tblSTAAT WHERE Land_ID = " & Me.cboLand.SelectedItem, connection)

    The error reads: Conversion of type DataRowView to type integer is not valid.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Netherlands
    Posts
    817

    Re: Fill combobox depending on value of other combo

    I solved it using this code:

    Code:
     Public Sub SelectLand()
        Dim adapter As New OleDbDataAdapter("SELECT Land_ID,Land FROM tblLAND", connection)
        Dim table As New DataTable
        adapter.Fill(table)
    
        With Me.cboLand
          .DataSource = table
          .DisplayMember = "Land"
          .ValueMember = "Land_ID"
                .SelectedIndex = 1
        End With
      End Sub
    
        Public Sub SelectStaat()
            Dim cmd As New OleDbCommand("SELECT Staat_ID,Land_ID,Staat FROM tblSTAAT WHERE Land_ID =?", connection)
            cmd.Parameters.Add(New OleDbParameter("Land_ID", OleDbType.VarChar))
            cmd.Parameters("Land_ID").Value = cboLand.SelectedValue.ToString
            Dim adapter As New OleDbDataAdapter(cmd)
            Dim table As New DataTable
    
            adapter.Fill(table)
    
            With Me.cboStaat
                .DataSource = table
                .DisplayMember = "Staat"
                .ValueMember = "Staat_ID"
                '.SelectedIndex = 1
            End With
        End Sub
    
        Private Sub cboLand_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLand.SelectedIndexChanged
            Me.txttest.Text = Me.cboLand.SelectedValue.ToString
            Call SelectStaat()
    
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width