My Assistant
![]() ![]() |
|
|
Apr 5 2011, 10:38 AM
Post
#1
|
|
|
UtterAccess Enthusiast Posts: 95 |
I have 2 unbound list boxes on my form.
The first list box is based off a query and lists the States. I would like the second list box to be populated with cities based off of what states the user selects. I can not get my code to work. Can anyone help? Private Sub lstStates_AfterUpdate() Dim varItem As Variant Dim strCriteria As String Dim strSQL As String For Each varItem In Me!lstStates.ItemsSelected strCriteria = strCriteria & ",'" & Me!lstStates.ItemData(varItem) & "'" Next varItem strCriteria = Right(strCriteria, Len(strCriteria) - 1) strSQL = "SELECT * FROM tblStatesCites " & _ "WHERE tblStatesCites.States IN(" & strCriteria & ");" lstCities.ListSource = Array(strSQL) End Sub |
|
|
|
Apr 5 2011, 10:46 AM
Post
#2
|
|
|
Utterly Yorkshire and Forum/Wiki Editor Posts: 15,882 From: Devon UK |
Hi
try changing this line lstCities.ListSource = Array(strSQL) to CODE Me.lstCities.RowSource = strSQL
|
|
|
|
Apr 5 2011, 10:49 AM
Post
#3
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
List boxes don't have a ListSource property: they have a RowSource property. As well, your Array statement is incorrect: you'd need to have the recordset returned by the SQL string in order for it to be doing anything.
Try: CODE Private Sub lstStates_AfterUpdate() Dim varItem As Variant Dim strCriteria As String Dim strSQL As String If Me!lstStates.ItemsSelected.Count > 0 Then For Each varItem In Me!lstStates.ItemsSelected strCriteria = strCriteria & ",'" & Me!lstStates.ItemData(varItem) & "'" Next varItem strCriteria = Mid(strCriteria, 2) strSQL = "SELECT * FROM tblStatesCites " & _ "WHERE tblStatesCites.States IN(" & strCriteria & ");" Else strSQL = "SELECT * FROM tblStatesCites " End If lstCities.RowSource = strSQL lstCities.RowSourceType = "Table/Query" End Sub You might consider putting an ORDER BY clause in your SQL statement to be more user-friendly. As well, it's generally considered a bad idea to use SELECT *, especially for the RowSource of a list box or combo box. Consider explicitly listing the fields of interest. |
|
|
|
Apr 5 2011, 11:00 AM
Post
#4
|
|
|
UtterAccess Enthusiast Posts: 95 |
Thank you so much! And good idea about the ORDER. I will do that too.
Jodi |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 20th May 2013 - 02:44 PM |