• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 814
  • Last Modified:

filter values in a combo box based upon selection in another combo box

Alright, I am transitioning over to Visual Studios from VBA and I'm still working though some of the differences.

I have a data driven application connected to an MSSQL database.  I have a form that I am using to allow the end user to post to it.

What I am trying to do right now is I have one combo box with a list of abbriviated State names (abStateComboBox) and another combo box that has a list of all US Counties (countyComboBox), both are bound to tables in the SQL.  (tblState and tblCounties respectively)  the counties table also includes the corresponding abbreviated state names associated with the county.

All I need to do is simply filter the counties based upon the selected text in the stateComboBox so that only the counties applying to that abbreviated state will apply.  It's a very simple SQL query, but I am not sure how to translate it into VB.Net properly.

Thanks for the help!
0
gwarcher
Asked:
gwarcher
  • 5
  • 4
1 Solution
 
technofileCommented:
What you need to do is create a new query on the event selectedvaluechanged on the state drop box and repopulate your county box. In the properties window there is a lightingbolt button that shows all the events, double click on the selectedvaluechanged place and it will create a subrouting for you to program in.
0
 
gwarcherAuthor Commented:
How do I in VB Syntax set the variable from the first stateComboBox in the select statement?
0
 
bhagwantsinghCommented:
If your abStateComboBox is bounded with the datatable and you assigned ValueMember property with CountryCode of the state than you can easily search the country without firing SQL query to the MSSQL server. You can filter your countyComboBox using its DefaultView property.

e.g.
countyComboBox.DataSrouce.DefaultView = CountryID = COUNTRYID

This statement will filter your country combobox according the state you selected. You can do this thing with counties table also. You must bound your countryComboBox with the datatable/dataset.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
technofileCommented:
str = cboState.SelectedText

will give you the value that you see in the drop down box.
0
 
gwarcherAuthor Commented:
I'm still not 100% clear. What would be the most efficient means of taking the value of the selected text from cboState and then filtering my dataset (table adapter?) to only show counties with the related state variable?

I apologize for not fully getting it just yet.
0
 
technofileCommented:
How are you filling your state combobox?
0
 
gwarcherAuthor Commented:
Attached is the picture and the code from the Main Form Load Event
Me.TblStatesTableAdapter.Fill(Me.MartinezContributionsDataSet.tblStates)
        Me.AbStateComboBox.Text = "NM"

Open in new window

stateComboBox.png
0
 
technofileCommented:
Ok, that is what I thought, what you need to do is modify your TblCountyBindingSource, or whatever you call it in the statechange even that I first mentioned there should be a query property or sqlquery property or something like that that you can change to something like "Select countycode, county from county where state = '" & cboState.SelectedText & "'". Right now if you read it while your program is running it should say something like above without the where clause. I personally don't use databinding because it is very limited and many times can cause a whole lot of problems.

I would take the time to learn about the sqlclient, the sqlclient.dataadapter, and datatables
http://www.startvbdotnet.com/ado/sqlserver.aspx
http://vb.net-informations.com/ado.net-dataproviders/ado.net-sqldataadapter.htm
http://functionx.com/vbnet/databases/datatable.htm

Once you understand these few things you will be able to do whatever you want with vb and databases much easier and if it can be done you will be able to do it.
0
 
gwarcherAuthor Commented:
Thanks technofile.  Those articles I assume are to replace the binding source with the sql classes mentioned?
0
 
technofileCommented:
Yes, binding allows you to simply view data but once you start manipulating it, or even something like filtering you have to start manipulating the bindings. The sql classes mentioned in the last post allows you, in my opinion a more flexible solution to dealing with databases.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now