Consulting

Results 1 to 4 of 4

Thread: Filter a Form based on a List Box

  1. #1

    Filter a Form based on a List Box

    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
    Last edited by Aussiebear; 04-13-2023 at 12:47 PM. Reason: Adjusted the code tags

  2. #2
    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.
    Last edited by Aussiebear; 04-13-2023 at 12:50 PM. Reason: Added code tags

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Are you sure WhereCrit is set correctly?

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    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]
    Last edited by Aussiebear; 04-13-2023 at 12:50 PM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •