PDA

View Full Version : Filter a Form based on a List Box



brorick
04-08-2005, 02:25 PM
I have come across coding for filtering a report based on a list box, but I have had no success locating any code for filtering a form based on a list box. I would like a seperate form to open based on the selected item from the list box. Does anyone have an idea how this could be accomplished? Any help is always appreciated. Thank you in advance.


Private Sub CmdRpt_Click()
'Run a report displaying only the records chosen
'by the user in the form's listbox.
Dim v As Variant
Dim Frm As Form
Dim ctl As Control
Dim theId As Long
Dim WhereCrit As String
'If nothing is selected, notify user...
If Me.LstFindings.ItemsSelected.Count = 0 Then
MsgBox "Please select a supplier or two.", vbExclamation, "No Supplier Selected"
Exit Sub
End If
'Assign form and control to object variables.
Set Frm = Forms!FrmSampleList
Set ctl = Frm!LstFindings
'Begin building Where string.
WhereCrit = "SupplierID = "
'Add each selected item to the WHERE string.
For Each v In ctl.ItemsSelected
'Coordinates: 1st column (0); row v
'where v changes for each round of the loop.
theId = ctl.Column(0, v)
'Tag on to string.
WhereCrit = WhereCrit & theId & " OR SupplierID = "
Next v
'Loop ends; selected items are now accounted for...
'Clean-up the Where string by removing the trailing text.
WhereCrit = Left(WhereCrit, Len(WhereCrit) - 17)
'By default, the Suppliers Report returns all records from the Suppliers Table.
'Here the Suppliers Report is opened using the 'Where clause' to filter it down _
to only the items selected in the listbox.
DoCmd.OpenReport "RptSuppliers", acViewPreview, , WhereCrit
End Sub

brorick
04-09-2005, 07:00 AM
I replaced the line


DoCmd.Openreport "RptSuppliers", acViewPreview, , WhereCrit

with


DoCmd.OpenForm "frmSupplierDetail", acNormal, WhereCrit

and the form opens successfully, I just can't seem to get the form to filter based on the WhereCrit (ID). The form shows all records. I am a step closer, but I am still not there. Any recommendations? Thanks.

Norie
04-09-2005, 10:12 AM
Are you sure WhereCrit is set correctly?

OBP
04-10-2005, 09:11 AM
This should be fairly simple to achieve. Put the value selected in the list box in to a text box on the same form using a simple afterupdate code. Then use the Field data in the docmd statement like this -


DoCmd.OpenForm "rmSupplierDetail", , , "[ID you are looking for]=" & Me![ID]