Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there,

Sorry to ask a silly/stupid question...

I'm new to all this and i'm in the process of learning VB .NET using Visual Studio 2010...

I just had a question because I cannot seem to find the answers anywhere on the internet... also we are not upto this part in class yet..

Say I have 2 combo boxs... cmb1 and cmb2.. both are datasets from an access table..

how do I make it so cmb2 refreshes based on the value of cmb1?

eg; if cmb1 is say, period 1, then i want cmb2 to only show results bases on items I have in period 1?

Any help would be greatly appreciated

Ross the newb
Posted

What you seem to want is called a cascading drop down. Search for that term and you will find plenty of info. Its more complicated than there is space here to describe adequately
 
Share this answer
 
Comments
Forge199 27-Oct-11 22:04pm    
Hi, thanks for getting back to me...

isn't that just for web based applications?
[no name] 27-Oct-11 22:09pm    
You didn't specify the environment, but no the technique may differ but the theory is the same.
I wrote the following subrutin which will help you to refresh Second ComboBox based on the value selected on the first Combobox.
Just Copy this code in your application and call this method on ComboBox1_SelectedIndexChnged.
VB
Public Sub FillCombo(ByVal idfiles As String, ByVal table As String, ByVal Combo1 As ComboBox, ByVal Combo2 as ComboBox)
        objda.SelectCommand.CommandText = "Select " & idfiles & " FROM " & table & " Where FieldName=" & Combo1 & ""
        Try 
            dr = objda.SelectCommand.ExecuteReader   'Exectuing query
        Catch ex As OleDb.OleDbException
            MessageBox.Show(ex.ToString)       'Providing Error Message
            ErrorLog(ex.ToString)              'Creating ErrorLog
        End Try
        While dr.Read                          'Reading DataReader Items
            Combo2.Items.Add(dr.Item(0))     'Adding Items Into ComboBox
        End While
        dr.Close()                         'Closing DataReader Object
    End Sub

Example for calling above method
VB
FillCombo("FieldName", "DatabaseTableName", ComboBox1,ComboBox2)
 
Share this answer
 
Comments
[no name] 28-Oct-11 8:06am    
While dr.Read? How about databinding?

Your DataReader object will also remain open if an exception is throw since the close is not in a finally block or in the catch. Better to use a using statement to ensure it is closed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900