ComboBox Auto Selecting next ComboBox (1 Viewer)

mcsacks

New member
Local time
Today, 05:46
Joined
Nov 27, 2006
Messages
3
Hi,

What I would like to do is the following:
I have 2 combo boxes, Branch ID and Division ID.

What I would like to happen is that when a user makes a selection in the Branch ID ComboBox that the field in the Division ID is automatically calculated for the user.

ie)
If you select "Mapping" in Branch ID ComboBox then Division ID is "Land and Water"
And if you select "Spatial" in Branch ID ComboBox then Division ID is "Natural Resources"

And so on....


Thanks
 

skea

Registered User.
Local time
Today, 15:46
Joined
Dec 21, 2004
Messages
342
How are the comboboxes populated?
Are the two fields in the same table? if so then just write an SQL statement that brings up a resultset of one value by using the executescarlar method of the command object. The criteria from the SQL will obviously come from the selection in the first combobox.
Populate the second combobox with the result from the executeScalar method.
 

mcsacks

New member
Local time
Today, 05:46
Joined
Nov 27, 2006
Messages
3
The two comboBoxes are populated with data from different tables.

Sorry I am new to this, it all seems confusing :confused:
 

skea

Registered User.
Local time
Today, 15:46
Joined
Dec 21, 2004
Messages
342
Ok. here is a qucik and dirty way to do it. You have to change it to suit your needs.
Code:
Dim conn As OleDbConnection = New OleDbConnection(YourconnectionString)
        Dim str As String = "SELECT [Field1] FROM tblDivisionID WHERE([BranchID]='" & Combobox1.Text & "')"
        Dim cmd As OleDbCommand = New OleDbCommand(str, conn)
        Dim dr As OleDbDataReader
        Try
            conn.Open()
            'Execute the reader
            dr = cmdSQL.ExecuteReader
            Combobox2.Items.Clear() ' Clear the Items First
            While dr.Read()
                'Populate the second combobox
                Combobox2.Items.Add(dr.Item("Field1").ToString)
            End While
        Catch ex As Exception
            'Raise exception if there is an error somewhere
            MessageBox.Show(ex.Message)
        Finally
            ' Close and Dispose the objects from memory
            dr.Close()
            conn.Close()
            conn.Dispose()
            cmd.Dispose()
        End Try
 
Last edited:

skea

Registered User.
Local time
Today, 15:46
Joined
Dec 21, 2004
Messages
342
I would advise you to normalise your tables well and create an array class. Then populate your comboboxes from the array. Your combos may take two fields, the hidden one and the visible one. Then you would use the SelectedIndexChanged event to populate the second combobox using a uniquie hidden value assosiated to the chosen value in the 1st combobox. just like in access.
 

Users who are viewing this thread

Top Bottom