cascading List Boxes in forms (1 Viewer)

Finance

Registered User.
Local time
Yesterday, 16:00
Joined
Jul 11, 2018
Messages
59
Hi,

is there a way to create cascading list boxes in a form?
both the list boxes draw data from the same table and they are extended list boxes for multiple selection.

List Box 1: Years
List Box 2: nature of Fees.

I want only those fees to be visible that are connected to the selected years. the other fees should not be visible.

Thanks!
 

Finance

Registered User.
Local time
Yesterday, 16:00
Joined
Jul 11, 2018
Messages
59
This is my code

Code:
Private Sub ListYears_AfterUpdate()
    Dim frm As Form_SelectionCriteriaForm, ctl As Control
    Dim varItem As Variant
    Dim strSQL As String
        
        [B][U]Set ctl = frm.ListYears[/U][/B]
        Set ctl2 = frm.lstNatureOfFees
        
        strSQL = "Select * from Nature_of_Fees where [Years_Word]="
        For Each varItem In ctl.ItemsSelected
            strSQL = strSQL & ctl.ItemData(varItem) & "or[Years_Word]="
        Next varItem
        
        strSQL = Left$(strSQL, Len(strSQL) - 12)
        
        Debug.Print strSQL
        ctl2.Requery
        
               
        
End Sub

theres an error in the highlighted part. Any suggestions?
Thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:00
Joined
Aug 30, 2003
Messages
36,125
That bit in the dim line looks wonky; I don't think you ever set the form variable. Try simply

Set ctl = Me.ListYears
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 19:00
Joined
May 21, 2018
Messages
8,528
Code:
Dim frm As Form_SelectionCriteriaForm
That does not open or instantiate the object.

either
Code:
Set frm = forms("SelectionCriteriaForm")
or
Code:
Dim frm as NEW Form_SelectionCriteriaForm
frm.visible = true

If already open do not use the latter.
 
Last edited:

Finance

Registered User.
Local time
Yesterday, 16:00
Joined
Jul 11, 2018
Messages
59
The nature of fees list box isnt sorting but the code isnt throwing an error.
COuld you look at the form on my database and maybe point out wheres the mistake. That would be a great help.
Thanks
 

Attachments

  • Database - After cascade.zip
    177.9 KB · Views: 43

JHB

Have been here a while
Local time
Today, 01:00
Joined
Jun 17, 2012
Messages
7,732
Replace what you have with the below:
Code:
Private Sub ListYears_AfterUpdate()
  Dim varItem As Variant
  Dim strSQL As String
        
  If Me.ListYears.ItemsSelected.Count Then
    strSQL = "Select ID_Number, Nature_of_Fees from CostSheet where [Years_Word] in ('"
    For Each varItem In Me.ListYears.ItemsSelected
      strSQL = strSQL & Me.ListYears.ItemData(varItem) & "','"
    Next varItem
    strSQL = Left(strSQL, Len(strSQL) - 2) & ") ORDER BY ID_Number"
  Else
    strSQL = "Select ID_Number, Nature_of_Fees from CostSheet ORDER BY ID_Number"
  End If
  Me.lstNatureOfFees.RowSource = strSQL
End Sub
 

Finance

Registered User.
Local time
Yesterday, 16:00
Joined
Jul 11, 2018
Messages
59
Thank you sooo much!!!!!!
It worked like a dream. Thanks!:)
 

Users who are viewing this thread

Top Bottom