Results 1 to 6 of 6
  1. #1
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117

    Trying to apply a dynamic multi-criteria filter and can't find a good reference

    Trying to do something that's a couple steps outside my comfort zone and so far, I can't find a useful example or explanation that I can wrap my head around.

    I have a continuous form open and I want to be able to filter the content based on three criteria. 1) The primary key, 2) The earliest date and 3) The latest date.



    I figure I could do it with a conglomerate of many If Then Else operations but that seems like it will be clumsy at best.

    I would also want to error check the two date entries to make sure the earliest does not occur after the latest.

    I had a thought about setting up three Dim variables in that section of code and apply default values and then when a value changes for any given field, change the that appropriate variable and reapply the filter.

    I would have to figure out how to reset the variables if the field value was removed, for instance, the Primary Key value could be set to null to bring back all entries just within the date range(s).

    I will start playing with stuff later but if there are some good examples anyone can point me to, that would be great.

    TIA

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Try my example Multiple Group & Filter apps
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Can't avoid If Then Else coding for this (unless you like queries with parameters).

    Here is another example http://allenbrowne.com/ser-62.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Vstar920AZ♣ is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2023
    Location
    Phoenix, AZ
    Posts
    11
    the Allenbrowne example above is much slicker that this, but you can go the recordsource route with SQL.
    dim sSQL as string
    sSQL = "SELECT * FROM TABLE WHERE PK=Me.cbo_PrimaryKey and DateInTbl >= Me.txb_StartDate and DateInTbl <= Me.txb_EndDate"
    'If you are uncomfortable with SQL, just create a query using the Query Editor and copy the SQL over. Adapt for criteria as needed.
    me.recordsoucre = sSQL
    me.requery 'May not be needed

    You will need a button or function to return to all records i.e. "SELECT * FROM TABLE"
    me.recordsoucre = sSQL

  5. #5
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Guys, I'm looking at the examples and getting the date filters to work isn't working. Not sure of my problem.

    I have reduced the actual code to apply just a single date filter but when applied, it isn't filtering as it should.

    Will clean things up a bit and post probably tomorrow current file with highlights of what and where in the app I'm trying to implement this.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything

    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.
    Added benefit is, if you still cannot see the error, you can copy and paste back here the output from the debug.print for someone else to spot it.
    Exaample:
    tt="Eg'g"
    ? dlookup("FoodID","tblFoods","FoodName = """ & tt & """")
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 9
    Last Post: 09-17-2024, 08:55 AM
  2. Replies: 4
    Last Post: 01-21-2023, 07:43 AM
  3. Replies: 2
    Last Post: 04-01-2022, 05:52 AM
  4. Filter By Form and Apply Filter Buttons
    By JustLearning in forum Forms
    Replies: 13
    Last Post: 02-18-2015, 01:01 PM
  5. Replies: 2
    Last Post: 02-25-2013, 10:47 AM

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