ADO & Musical Combos
Ok, here I am with my happy go lucky database of information and I am populating 2 combo boxes. My database is a database of network numbers that I want the user to make a selection from. The database is layed out like so:
State -> City -> AreaCode -> PhoneNumber -> NetworkID
I am only going to be using the following fields in the following order:
1. User selects State
2. User Selects City
3. Number Shows Up in Caption
On my Form I have 2 Combo boxes and a Caption field. I am using the following code to populate them like so:
option Explicit
Dim con as ADODB.Connection
Dim rs as ADODB.Recordset
private Sub Combo1_Click()
rs.MoveFirst
rs.Filter = "State > """
rs.Find "State = '" & Replace(Combo1.Text, "'", "''") & "'", , adSearchForward
If Not rs.EOF then
PopulateCity
End If
End Sub
private Sub form_load()
set con = new ADODB.Connection
set rs = new ADODB.Recordset
con.Open "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = c:\temp\dialup\locations.mdb;" & _
"Persist Security Info = false"
rs.Open "select * from locations", con, adOpenDynamic, adLockOptimistic
Do While rs.EOF = false
Combo1.AddItem rs!State
rs.MoveNext
Loop
End Sub
private Sub PopulateCity()
Do Until rs.EOF
Combo2.AddItem rs!City & " - " & rs!NetworkID
rs.MoveNext
Loop
End Sub
My Problems are such:
1. The 1st combo box adds all the states. I only want one of each states and not 10 or more of the same state.
2. After selecting the City, I want the Phone Number to be displayed in the caption box. I was wondering for some pointers on how I can do this.
3. If a user makes a mistake and selects the wrong state, I want them to be able to drop down the the combo box again and select another state and it update the records in the 2nd combo box. This code does not do that at this time.
Any suggestions would be greatly apperciated.
Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com
[2269 byte] By [
tschilbach] at [2007-11-15 16:16:12]

# 1 Re: ADO & Musical Combos
On questions:
1. For fill combo box 1 use RecordSet with source "Select distinct state from locations"
2. Use
private Sub Combo1_Click()
rs.Find "State = '" & Replace(Combo1.Text, "'", "''") & "' and City=" & Replace(Combo2.Text, "'", "''") & "'", , adSearchForward
If Not rs.EOF then
Label1.Text = rs!PhoneNumber
End If
End Sub
3. Replace function
private Sub PopulateCity()
Combo2.Clear
Do Until rs.EOF
Combo2.AddItem
rs!City & " - " & rs!NetworkID
rs.MoveNext
Loop
End Sub
Tower at 2007-11-10 0:59:12 >

# 2 Re: ADO & Musical Combos
Thank you for the post and suggestions. When I entered in the following code:
option Explicit
Dim con as ADODB.Connection
Dim rs as ADODB.Recordset
private Sub Combo1_Click()
rs.MoveFirst
rs.Find "State = '" & Replace(Combo1.Text, "'", "''") & "' and City=" & Replace(Combo2.Text, "'", "''") & "'", , adSearchForward
If Not rs.EOF then
Label1.Caption = rs!PhoneNumber
PopulateCity
End If
End Sub
private Sub form_load()
set con = new ADODB.Connection
set rs = new ADODB.Recordset
con.Open "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = c:\temp\dialup\locations.mdb;" & _
"Persist Security Info = false"
rs.Open "select distinct state from locations", con, adOpenDynamic, adLockOptimistic
Do While rs.EOF = false
Combo1.AddItem rs!State
rs.MoveNext
Loop
End Sub
private Sub PopulateCity()
Combo2.Clear
Do Until rs.EOF
Combo2.AddItem = rs!City & " - " & rs!Network
rs.MoveNext
Loop
End Sub
I encountered an error 3001 on the following line:
rs.Find "State = '" & Replace(Combo1.Text, "'", "''") & "' and City=" & Replace(Combo2.Text, "'", "''") & "'", , adSearchForward
This tells me that the line has a conflicting argument when trying to populated the Combo2 box. Any suggestions on beating this conflict? All of the ' and " marks are confusing me. Why do we use them in this line? I am not familliar with the FIND object in the recordset. Thank you in advance.
Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com
# 3 Re: ADO & Musical Combos
I don't think that you can use 2 conditions with the 'FIND' method.
rs.Find "State = '" & Replace(Combo1.Text, "'", "''") & "'", , adSearchForward,adbookmarkfirst
I don't think that this is what you want though. Find sets the current row of the recordset to the row that had been picked. You want to limit the recordset to only the cities that are associated with the state that was selected. I beleive you need to 'Filter' these rows out as there will be more than one row.
rs.Filter = "State = '" & Replace(Combo1.Text, "'", "''")
when using sql statements
All strings must be delimited with the tick mark.
numbers are not delimited with anything
dates are delimited with the pound sign '#'
I think that this is more what your after. Please correct me if it is not.
Option Explicit
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Combo1_Click()
rs.movefirst
rs.Filter "State = '" & Replace(Combo1.Text, "'", "''")
rs.movefirst
PopulateCity
End Sub
Private Sub Combo2_Click()
rs.movefirst
rs.Find "City = '" & Trim(Replace(Left$(Combo2.Text, InStr(Combo2, "-") - 2), "'", "''")) & "'", , adsearchforward, adbookmarkfirst
'That part is confusing. There is easier ways to single out the record.
'May need to modify you table to include an autonumber field if you don't
'have one
If Not rs.EOF Then
label1 = rs!Phonenumber
Else
MsgBox "Not Found"
End If
End Sub
Private Sub form_load()
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
con.open "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = c:\temp\dialup\locations.mdb;" & _
"Persist Security Info = false"
rs.open "select distinct state from locations", con, adOpenDynamic, adLockOptimisticm, adcmdtext
PopulateStateCombo
rs.open "Select * from locations", con, adOpenDynamic, adLockOptimistic, adcmdtext
End Sub
Private Sub PopulateCity()
Combo2.Clear
rs.movefirst
Do Until rs.EOF
Combo2.AddItem rs!City & " - " & rs!Network
rs.MoveNext
Loop
End Sub
Private Sub PopulateStateCombo()
Do Until rs.EOF
Combo1.AddItem rs!State
rs.MoveNext
Loop
End Sub
David Paulson
# 4 Re: ADO & Musical Combos
Thank you for the reworked code. I understand a bit more now. I am confused on why you have 2 querries in the Form_Load sub. I get a 3705 error when I run the completed Code. It tells me that the 2nd querry is NOT ALLOWED. Here is the sub:
private Sub form_load()
set con = new ADODB.Connection
set rs = new ADODB.Recordset
con.Open "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source = c:\temp\dialup\locations.mdb;" & _
"Persist Security Info = false"
rs.Open "select distinct state from locations", con, adOpenDynamic, adLockOptimistic, adCmdText
PopulateStateCombo
rs.Open "select * from locations", con, adOpenDynamic, adLockOptimistic, adCmdText
End Sub
I tried doing the code without the 2nd querry and I get a Compile Error on the following code line when I slect my state:
rs.Filter "State = '" & Replace(Combo1.Text, "'", "''")
What can we do to work these bugs out. Thank you in advance.
Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com
# 5 Re: ADO & Musical Combos
I put in 2 queries because if you use the distint keyword you will return recordset containing rows of states with no duplicates (needed only to populate the combo box). This is what you wanted, however there are also rows in the table that will have the same state, but different cities, so you would still need those rows also. I suppose you could have just filled the recordset once and filled the combo box with the duplicate states, and created some sort of algorithm to delete the duplicate entries. This was just one way to acheive that. As for the rs.Filter "State = '" & Replace(Combo1.Text, "'", "''") it looks like i forgot an = sign. Try this instead.
rs.Filter = "State = '" & Replace(Combo1.Text, "'", "''")
I hope you don't have many more problems as I never tested the code I gave you as I would have needed to create the database to text. As far as your first problem, maybe you'll need to close the recordset, set it to nothing, and reinitialize it again , to open it again.
rs.Open "select distinct state from locations", con, adOpenDynamic, adLockOptimistic, adCmdText
PopulateStateCombo
rs.close
set rs = nothing
set rs = new adodb.recordset
rs.Open "select * from locations", con, adOpenDynamic, adLockOptimistic, adCmdText
End Sub
David Paulson
# 6 Re: ADO & Musical Combos
David,
I have tried it both ways and it still gives me a 3001 error at: rs.filter = "state = '" & Replace(combo1.text, "'", "''")
I have emailed my code and database in hopes of getting this one thing to work LOL. Thank you in advance for all your help.
Timothy H. Schilbach
Alpha Omega Design Inc.
tschilbach@aodinc.com