UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

> Multi Select List Boxes - Populating A Second List Box From Selection In First List Box    
 
   
jodidew
post 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
Go to the top of the page
 
+
 
Start new topic
Replies (1 - 3)
Alan_G
post Apr 5 2011, 10:46 AM
Post #2

Utterly Yorkshire and Forum/Wiki Editor
Posts: 15,885
From: Devon UK



Hi

try changing this line

lstCities.ListSource = Array(strSQL)

to

CODE
Me.lstCities.RowSource = strSQL
Go to the top of the page
 
+
Doug Steele
post Apr 5 2011, 10:49 AM
Post #3

UtterAccess VIP
Posts: 17,643
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.
Go to the top of the page
 
+
jodidew
post 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 the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 23rd May 2013 - 10:29 PM