Question : Filtering subform results based on unbound listbox selections

There's other similar questions here but don't seem to be either working or exactly what my issue is.

I have multiple unbound (cascading) listboxes on the mainform, and I would like the subform to filter it's results based on the selections in the listboxes.  For example:

Main form:
-lst1 properties = tblAPK; tblADescription (only shows description)
-lst2 properties = tblBPK; tblBDescription (only shows descrition)
I'm not sure if it makes a difference but the lst2 display is filtered by what's selected in lst1.

Subform properties:  Details; Dates; Names; tblADesc; tblBPK

So the results on the subform would be filtererd when lst1 was selected to only tblADesc, and further filterd when lst2 was selected to only tblB selction.

Right now the only real "code" I have to speak of is requerying the cascasing lists:

Private Sub lstA_AfterUpdate()
Me.lstB.Requery
End Sub

Thanks

Answer : Filtering subform results based on unbound listbox selections

what about having a function in your main form.  Then after update of the list boxes you call the function, which will assign a recordsource to the subform.

Function FilterSubform()
dim strSQL as string
dim strWhere as string, strDelim as string
   
   strDelim = "":strwhere = ""
   If nz(lst1,0) > 0 then
          strWhere = "tblAPK = " & lst1
          strDelim = " And "
   end if

   If nz(lst2,0) > 0 then
          strWhere = strWhere & strdelim & "tblBPK = " & lst2
   end if

   if strwhere <> "" then strwhere = " Where " & strwhere
   strsql = "Select * From qry" & strwhere
   me!subform.form.RecordSource = strsql

End Function
Random Solutions  
 
programming4us programming4us