Chat with a LIVE Microsoft Access Expert!
 
       
 

         

   

Go Back   Access World Forums > Microsoft Access Discussion > Forms

 
 
Chat with a LIVE Microsoft Access Expert!
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-23-2007, 05:31 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
cascading multiple selection list boxes?

Has anyone ever done this?

I have two multiple selection list boxes, where the second one filters off the first one. So if the user picks 3 items from the first list box then a re-query should update the 2nd list box with the corresponding items.

I've tried to do this like they are combo boxes but it doesn't work. Any help would be great!!
Reply With Quote
Sponsored Links
  #2  
Old 07-23-2007, 06:10 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
btw I am doing this to run a query to have the list box as criteria. I have this working for one list box. So after I get the second list box working I should be able to have both boxes as criteria right?
Reply With Quote
  #3  
Old 07-23-2007, 07:31 AM
Banana Banana is offline
split with a cherry atop.
 
Join Date: Sep 2005
Posts: 5,386
Banana is just really niceBanana is just really niceBanana is just really niceBanana is just really niceBanana is just really nice
The thing is that if you set listbox's result as criteria, you will get only one selection, whether more than one were selected or not.

In order to get the complete listing, you'll need to loop through the selection, add it to a string then insert that string in the query's criteria.

You'll need to do VBA here. You may be able to get away with using the click event if it is not too intensive. Otherwise an double click or a button will be a good placeholder for the code.
__________________
If relation-valued attributes and arbitrarily complex types are wrong, then I don't wanna to be right!
Founder of 'Blame the Developers First' crowd.

How to get your questions answered promptly!
Reply With Quote
  #4  
Old 07-23-2007, 07:41 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
Banana, I see what you're saying and I have tried to build the Code myself. Here is what I have, but I'm not sure how I can re-query my list box with this code. Hopefully you can help me out.

Code:
Private Sub lstTrains_AfterUpdate()

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
   Set frm = Forms!MainForm
   Set ctl = frm.lstTrains
   strSQL = "Select * from StopOrder where [Train]="
   'Assuming long [Train] is the bound field in lb
   'enumerate selected items and
   'concatenate to strSQL
   For Each varItem In ctl.ItemsSelected
       strSQL = strSQL & ctl.ItemData(varItem) & " OR [Train]="
   Next varItem

   'Trim the end of strSQL
   strSQL = Left$(strSQL, Len(strSQL) - 12)
   
End Sub
Reply With Quote
  #5  
Old 07-23-2007, 07:46 AM
Banana Banana is offline
split with a cherry atop.
 
Join Date: Sep 2005
Posts: 5,386
Banana is just really niceBanana is just really niceBanana is just really niceBanana is just really niceBanana is just really nice
If the query is working correctly and all you need to do is requery the listbox, then you just need this:

Code:
ctl.Requery
Edit: I just noticed there's nothing about updating the rowsource of the listbox... If you aren't updating the SQL, nothing will change.

Code:
ctl.Rowsource = strSQL
As matter of note, it may be more efficient to use a stored parameter query, and insert the strSQL in the parameter instead of re-writing the rowsource's SQL each time you update it.
__________________
If relation-valued attributes and arbitrarily complex types are wrong, then I don't wanna to be right!
Founder of 'Blame the Developers First' crowd.

How to get your questions answered promptly!
Reply With Quote
  #6  
Old 07-23-2007, 07:52 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
Quote:
Originally Posted by Banana View Post
If the query is working correctly and all you need to do is requery the listbox, then you just need this:

Code:
ctl.Requery
Don't I need to re-query the other listbox? Well here is my updated code, but it still isn't working..

Code:
Private Sub lstTrains_AfterUpdate()

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String

   Set frm = Forms!mainform
   Set ctl = frm.lstTrains
   Set ctl2 = frm.LstOS
   
   strSQL = "Select * from StopOrder where [Train]="
   'Assuming long [Train] is the bound field in lb
   'enumerate selected items and
   'concatenate to strSQL

   For Each varItem In ctl.ItemsSelected
       strSQL = strSQL & ctl.ItemData(varItem) & " OR [Train]="
   Next varItem

   'Trim the end of strSQL
   strSQL = Left$(strSQL, Len(strSQL) - 12)
   
   Debug.Print strSQL
   ctl2.Requery
   
End Sub
My debug.print gives me...
Select * from StopOrder where [Train]=204 OR [Train]=205 OR [Train]=206 OR [Train]=207
Reply With Quote
  #7  
Old 07-23-2007, 07:59 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
Quote:
Originally Posted by Banana View Post
Edit: I just noticed there's nothing about updating the rowsource of the listbox... If you aren't updating the SQL, nothing will change.

Code:
ctl.Rowsource = strSQL
As matter of note, it may be more efficient to use a stored parameter query, and insert the strSQL in the parameter instead of re-writing the rowsource's SQL each time you update it.
does the ct.rowsource=strsql go at the end of my code?

So in essence would this stored query be a temp query?
Reply With Quote
  #8  
Old 07-23-2007, 08:12 AM
Banana Banana is offline
split with a cherry atop.
 
Join Date: Sep 2005
Posts: 5,386
Banana is just really niceBanana is just really niceBanana is just really niceBanana is just really niceBanana is just really nice
Yes, that's correct; just insert it before you requery the listbox. We'll concentrate on getting it to work before we try parameter query.

You only requery the other listbox if that listbox's content has changed, too.
__________________
If relation-valued attributes and arbitrarily complex types are wrong, then I don't wanna to be right!
Founder of 'Blame the Developers First' crowd.

How to get your questions answered promptly!
Reply With Quote
  #9  
Old 07-23-2007, 08:23 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
When I do this... ctl.RowSource = strSQL

it blanks all the values out of my Train Listbox.

to give you a better picture the end of my code looks like this
Code:
   strSQL = Left$(strSQL, Len(strSQL) - 12)
   
   Debug.Print strSQL
   ctl.RowSource = strSQL
   ctl.Requery
Reply With Quote
  #10  
Old 07-23-2007, 08:27 AM
Banana Banana is offline
split with a cherry atop.
 
Join Date: Sep 2005
Posts: 5,386
Banana is just really niceBanana is just really niceBanana is just really niceBanana is just really niceBanana is just really nice
Hmm.

And the strSQL is returning the correct SQL when you look at the immediate window, no?

If so, copy and paste it into the listbox's rowsource in design mode then run it to see if it's showing as expected.
__________________
If relation-valued attributes and arbitrarily complex types are wrong, then I don't wanna to be right!
Founder of 'Blame the Developers First' crowd.

How to get your questions answered promptly!
Reply With Quote
  #11  
Old 07-23-2007, 08:32 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
ahhh I totally forgot I need to put quotes around the train numbers... in other words.

[Train]="204" OR [Train]="205" OR [Train]="206" OR [Train]="207"


I'll try and fix this and give you an update! thanks with everything so far.

Edit: Nevermind !!

Last edited by kidrobot; 07-23-2007 at 08:45 AM..
Reply With Quote
  #12  
Old 07-23-2007, 08:47 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
AWESOME. I have it working!!!

Final code...
Code:
Private Sub lstTrains_AfterUpdate()

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String

   Set frm = Forms!mainform
   Set ctl = frm.lstTrains
   Set ctl2 = frm.LstOS
   
   strSQL = "Select * from StopOrder where [Train]="
   'Assuming long [Train] is the bound field in lb
   'enumerate selected items and
   'concatenate to strSQL

   For Each varItem In ctl.ItemsSelected
       strSQL = strSQL & "'" & ctl.ItemData(varItem) & "'" & " OR [Train]="
   Next varItem
   Debug.Print strSQL

   'Trim the end of strSQL
   strSQL = Left$(strSQL, Len(strSQL) - 12)
   
   Debug.Print strSQL
   ctl2.RowSource = strSQL
   ctl.Requery
   ctl2.Requery
End Sub
Reply With Quote
  #13  
Old 07-23-2007, 09:07 AM
Banana Banana is offline
split with a cherry atop.
 
Join Date: Sep 2005
Posts: 5,386
Banana is just really niceBanana is just really niceBanana is just really niceBanana is just really niceBanana is just really nice
Glad you got it to work.

Don't forget to delete all debug.print from the final code.
__________________
If relation-valued attributes and arbitrarily complex types are wrong, then I don't wanna to be right!
Founder of 'Blame the Developers First' crowd.

How to get your questions answered promptly!
Reply With Quote
  #14  
Old 07-23-2007, 10:07 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
One more thing. Sometimes the 2nd list box will show the same value twice. Is there any way I can make it select Unique values?

I tried

strSQL = "Select DISTINCT* from StopOrder where [Train]="

which doesn't seem to work.
Reply With Quote
  #15  
Old 07-23-2007, 10:15 AM
kidrobot kidrobot is offline
Registered User
 
Join Date: Apr 2007
Location: GA
Posts: 410
kidrobot is on a distinguished road
never mind I figured it out =)
Reply With Quote
Sponsored Links
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
List Box selection problem mbond Modules & VBA 4 03-22-2005 04:15 AM
Multiple cascading boxes on subform jgrayson Forms 1 01-11-2005 11:02 PM
List / Combobox Multiple Selection MartijnAn Forms 2 11-25-2004 04:35 AM
List Box Multiple Selection for query criteria carvind Queries 8 08-28-2001 12:23 AM
Event driven list boxes kettch Forms 1 02-28-2001 03:12 PM


All times are GMT -8. The time now is 12:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
(c) copyright 2009 Access World