473,699 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combo Box from SQL

Hi all
this question might be more SQL oriented or at least database
structure...
i created a table in my SQL which contains 2 columns:
1. state
2. country

the way i input the data is:
State Country
UK London
UK Manchaster
USA NY
USA Texas
etc'

then, created two combo boxes on a simple form (visual basic 2005)
added connection string to the SQL and bound them to the combo boxes.
first STATE to combo "State" then COUNTRY to combo "Counrty"

now STATE: contains a list of states(UK, USA etc') and COUNTRY
contains a list of countries.(Lond on, NY etc')
what i want to create is this:
when a user choose from the combo the UK state, then only the relevant
countries will
appear in the other combo box..IE: London, Manchester etc'

By the question you probably understand i don't know much about
programming, so go easy on me :)

Thanks,
50.

May 8 '07 #1
2 4877
On May 8, 5:43 am, 5070...@gmail.c om wrote:
Hi all
this question might be more SQL oriented or at least database
structure...
i created a table in my SQL which contains 2 columns:
1. state
2. country

the way i input the data is:
State Country
UK London
UK Manchaster
USA NY
USA Texas
etc'

then, created two combo boxes on a simple form (visual basic 2005)
added connection string to the SQL and bound them to the combo boxes.
first STATE to combo "State" then COUNTRY to combo "Counrty"

now STATE: contains a list of states(UK, USA etc') and COUNTRY
contains a list of countries.(Lond on, NY etc')
what i want to create is this:
when a user choose from the combo the UK state, then only the relevant
countries will
appear in the other combo box..IE: London, Manchester etc'

By the question you probably understand i don't know much about
programming, so go easy on me :)

Thanks,
50.
You can do this with parameterized databindings, but it might be
easier to do it manually if you are only worrying about the combobox.

Something Like:

' Typed in message

Imports System.Data.Sql Client

Private Sub stateComboBox_S electedValueCha nged(sender as object, e as
EventArgs) Handles stateComboBox.S electedValueCha nged
Dim conn As New SqlConnection(" your conn string") ' Use OleDb or
ODBC objects if needed
Using (conn)
conn.Open()
Dim com As SqlCommand = conn.CreateComm and()
Using (com)
com.CommandType = CommandType.Tex t
com.CommandText = String.Format(" Select Country From
MyTable Where State = '{0}' Order By Country Asc", stateComboBox.T ext)
Dim dr As SqlDataReader = com.ExecuteRead er()
Using (dr)
countryComboBox .Clear()
While (dr.Read())

countryComboBox .Items.Add(dr.G etValue(0).ToSt ring())
End While
End Using
End Using
End Using
End Sub

That code might need some work, but it should be close. Let me know if
you need further help.

Thanks,

Seth Rowe

May 8 '07 #2
On 8 mayo, 05:43, 5070...@gmail.c om wrote:
Hi all
this question might be more SQL oriented or at least database
structure...
i created a table in my SQL which contains 2 columns:
1. state
2. country

the way i input the data is:
State Country
UK London
UK Manchaster
USA NY
USA Texas
etc'

then, created two combo boxes on a simple form (visual basic 2005)
added connection string to the SQL and bound them to the combo boxes.
first STATE to combo "State" then COUNTRY to combo "Counrty"

now STATE: contains a list of states(UK, USA etc') and COUNTRY
contains a list of countries.(Lond on, NY etc')
what i want to create is this:
when a user choose from the combo the UK state, then only the relevant
countries will
appear in the other combo box..IE: London, Manchester etc'

By the question you probably understand i don't know much about
programming, so go easy on me :)

Thanks,
50.
Hi, there

Try this. This solution is using Ole DB Connection. You must set the
UserID, Password, Database and Data server.

cb1(combobox) = State
cb2(combobox) = Country

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
Try
Dim OleDBQuery As String = "SELECT DISTINCT State FROM
Table1"
Dim OleDBConn As New
System.Data.Ole Db.OleDbConnect ion("Provider=S QLOLEDB.1;Persi st
Security Info=False;User ID=<user>;Passw ord=<pwd>;Initi al
Catalog=<databa se>;Data Source=<dataser ver>")
Dim OleDBComm As New
System.Data.Ole Db.OleDbCommand (OleDBQuery, OleDBConn)
Dim OleDBRec As System.Data.Ole Db.OleDbDataRea der

OleDBConn.Open( )
OleDBRec = OleDBComm.Execu teReader()

While OleDBRec.Read
cb1.Items.Add(O leDBRec("State" ))
End While

OleDBRec.Close( )
OleDBConn.Close ()
Catch dbError As Exception
MsgBox(dbError. Message, MsgBoxStyle.OKO nly, "Error")
End Try
End Sub

Private Sub cb1_SelectedInd exChanged(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
cb1.SelectedInd exChanged
Try
cb2.Items.Clear ()
Dim OleDBQuery As String = "SELECT Country " + _
"FROM Table1 " + _
"WHERE State = '" + cb1.Text +
"'"
Dim OleDBConn As New
System.Data.Ole Db.OleDbConnect ion("Provider=S QLOLEDB.1;Persi st
Security Info=False;User ID=<user>;Passw ord=<pwd>;Initi al
Catalog=<databa se>;Data Source=<dataser ver>")
Dim OleDBComm As New
System.Data.Ole Db.OleDbCommand (OleDBQuery, OleDBConn)
Dim OleDBRec As System.Data.Ole Db.OleDbDataRea der

OleDBConn.Open( )
OleDBRec = OleDBComm.Execu teReader()

While OleDBRec.Read
cb2.Items.Add(O leDBRec("Countr y"))
End While

OleDBRec.Close( )
OleDBConn.Close ()
cb2.SelectedInd ex = 0

Catch dbError As Exception
MsgBox(dbError. Message, MsgBoxStyle.OKO nly, "Error")
End Try
End Sub

:)

May 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4314
by: vgrssrtrs | last post by:
<html> <head> <script language="JavaScript"> <!-- /* *** Multiple dynamic combo boxes *** by Mirko Elviro, 9 Mar 2005 *** ***Please do not remove this comment
4
4280
by: Heather | last post by:
Hi I am desparately looking for advice in relation to storing the results after selecting items from two combo boxes on a Referral form. The first combo box 'ctl Type' displays a full list of Agency Types, then the 2nd combo box displays a list of Organisations, depending upon the Agency Type Selected. The first combo box 'ctlType' (Unbound), Row Source: to field in 'AgencyReferralType'.
6
3549
by: Support4John | last post by:
a2k (9.0.6926) SP-3 Jet 4.0 SP-7 I have a form with combo box field that allows the user to select from the combo box or type in the field value that may or maynot be in the combo box field. In the After Update Event of the combo box field how can I tell if the field value was selected from open combo box or typed in by the user? Thanks, John
2
2173
by: Jeff Mason | last post by:
I'm observing some strange behavior when I use a bound combo box in conjunction with the combo's anchor property. I define a form which contains just a textbox and a combo box. The text box is placed before the combo in taborder, and is there only so the focus will go someplace other than the combo box when the form is displayed. In the form load event, I have the following code to create an arraylist and bind the combo to it: Dim a As...
3
3652
by: hmiller | last post by:
Hey everyone, I am having a hell of a time trying to set this menu system up. Here's what I'm trying to do. Combo Box One; is populated by names under properties "row source" "Phase 1" through "Phase 10" (there are 10 Phases I want to sort from) Once the phase has been selected a second combo box would populate.
9
6759
by: Edwinah63 | last post by:
Hi everyone, Please let there be someone out there who can help. I have two BOUND combo boxes on a continuous form, the second being dependent on the first. I have no problem getting the second combo to change depending on what values the user selects in the first box, it's just that every time the user changes the first combobox, the second combobox FOR EVERY RECORD goes blank.
2
2446
by: kiranmn75 | last post by:
I want to dynamically populate a combo box through javascript. Data is coming from a array. Sometimes data list may contain items in excess of 2000. Explorer takes more than 5 seconds to populate the combo (for a list of 2500). Can anybody optimize the code. I tried Duffs devise instead of 'for loop', but no improvement.
6
3677
by: Dave | last post by:
I want to put the information that the user selects in my combo boxes into a subform that lies on the same form as the combo boxes. Thanks for your help already, Dave
2
7047
by: biganthony via AccessMonster.com | last post by:
Hi, I decided to install Office 2003 Service Pack 3 on my home computer to test (in full knowledge that there may be some issues with it). After installation, I have noticed that with a small database I wrote for home, the combo boxes and listboxes no longer display the bound column. For example, on a form I have a combo box based on a table called 'names'. The two columns in the combo box are ID and Surname. The combo box and list box...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.