Results 1 to 5 of 5
  1. #1
    WDC is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2011
    Posts
    6

    Pass multiple dates selected from a multiselect listbox to the criteria in a filter query

    Having trouble here with editing a borrowed sub that was used for text so that it will work with dates. I have a table named SriDates that is used as the Row Source for a multi-select list box named List_SriDateSelector. This list box is in a form named SriDateSelector, which has a command button, named command2, that launches the vba below on click. I need for the script to pass the dates selected from the list box to the Criteria of a filter query named SriDatesFltr. The dates in the SriDates table are in short date format, and these can be easily sorted for selection in the list box, and they are in sequential order in the output Excel file (which uses a crosstab query as its source). The problem is with getting the script edited so that it will not pass the multiple selected dates as text into the criteria of the filter query, but will pass them as dates.


    Here's that script:
    Code:
    Private Sub Command2_Click()
       Dim Q As QueryDef, DB As Database
       Dim Criteria As String
       Dim ctl As Control
       Dim Itm As Variant
       ' Build a list of the selections.
       Set ctl = Me!
    [List_SriDateSelector]
       For Each Itm In ctl.ItemsSelected
          If Len(Criteria) = 0 Then
             Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
          Else
             Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
              & Chr(34)
          End If
       Next Itm
       If Len(Criteria) = 0 Then
          Itm = MsgBox("You must select one or more items in the" & _
            " list box!", 0, "No Selection Made")
          Exit Sub
       End If
       ' Modify the Query.
       Set DB = CurrentDb()
       Set Q = DB.QueryDefs("SriDatesFltr")
       ' Modify the Query.
       Set DB = CurrentDb()
       Set Q = DB.QueryDefs("SriDatesFltr")
       Q.SQL = "Select [SriDates].[SriTestDate]From [SriDates]WHERE [SriDates].[SriTestDate] IN (" & Criteria & _
         ");"
       Q.Close
    
    End Sub
    Thanks for any help on this one
    Last edited by orange; 03-27-2018 at 10:52 AM. Reason: added code tags

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    you could do a bunch of programming or,
    make a tPick table to pick the items you want to use.
    Connect tPick table to the data table on the common field. This will filter the data.

    dbl-click runs append query to add to tPick
    del key run delete query to remove from tPick
    final button runs the query

    similar to:
    Click image for larger version. 

Name:	pick state-lbl.png 
Views:	30 
Size:	34.2 KB 
ID:	33304

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    For a date/time field, you'd want "#" instead of Chr(34) as the delimiter.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    i use a function like below for Multi-select listboxes.
    Code:
    Public Function getLBX(LBx As ListBox, Optional colmn As Integer = 0, Optional delim As String = "", Optional Trm As Integer = 1) As String
    'arguements:
    'LBx = Name of your listbox
    'colmn = the column in listbox to be returned
    'delim = Any delimiter around value
    'trm = number of characters to remove from end
    
    
        Dim strList As String
        Dim varSelected As Variant
    
    
        If LBx.ItemsSelected.Count = 0 Then
            'MsgBox "You haven't selected anything"
        Else
            For Each varSelected In LBx.ItemsSelected
                strList = strList & delim & LBx.Column(colmn, varSelected) & delim & ","
            Next varSelected
            strList = Left$(strList, Len(strList) - Trm)
            'MsgBox "You selected the following items:" & vbCrLf & strList
        End If
    
    
        getLBX = strList
    
    
    End Function
    you can then call it with something like

    criteria = getLBx(me. List_SriDateSelector,,"#")
    it wil return a string of dates Like- #3/1/2018#,#3/31/2018#,#3/30/2018#,#2/6/2018#

  5. #5
    WDC is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2011
    Posts
    6
    Thanks pbaldy and ranman 256. I made a copy of the access app and tried one recommendation in the original, and the other recommendation in the other. Both worked!, thanks again

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 16
    Last Post: 03-31-2020, 10:19 AM
  2. Replies: 1
    Last Post: 05-21-2017, 12:31 AM
  3. Replies: 4
    Last Post: 06-24-2013, 07:34 AM
  4. Replies: 4
    Last Post: 02-14-2013, 09:33 PM
  5. Query Criteria with Multiple Dates
    By Jojojo in forum Queries
    Replies: 3
    Last Post: 10-08-2011, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums