Results 1 to 5 of 5
  1. #1
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43

    Multiple DoCmd.ApplyFilter filteers on form

    Hello, all!

    In short what I am trying to achieve is to filter a bound form's shown records using the DoCmd.ApplyFilter method. This is form is bound to a table that houses information on preventative maintenance actions performed on machinery. The form itself is just a review form so the records can be checked to see if they were done and when.



    I have 3 controls in the form I am trying to use as filters:

    MachineComboBox
    StartDateTextBox
    EndDateTextBox

    If I try a single filter (filtering by machine number) using the following code it works fine:

    Code:
    DoCmd.ApplyFilter , "[Machine]='" & Me.MachineComboBox & "'"
    However, when I try to filter by machine number as well as by date range (the values between StartDateTextBox and EndDateTextBox) I get a syntax error:

    Code:
    DoCmd.ApplyFilter , "[Machine]='" & Me.MachineComboBox & "'"
    DoCmd.ApplyFilter , "[PerformedOn] >='" & Me.StartDateTextBox & "'" and <='" & Me.EndDateTextBox & "'"
    So my questions are, is trying to perform two filters in this way the wrong way to go about it, and in the second line of above code where am I going wrong. Admittedly (and obviously), I am horrible at using single and double quotes properly.

    Thank you!


    Edit: The textboxes are formatted as Short Date and the table column is formatted as Date/Time.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    DoCmd.ApplyFilter , "[PerformedOn] >=#" & Me.StartDateTextBox & "# and [PerformedOn] <= #" & Me.EndDateTextBox & "#"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    Code:
    sub btnFind_click()
    dim sWhere as string
    
    
    sWhere = "1=1"
    if not IsNull(txtBox1) then sWhere = sWhere & " and [field1]='" & me.txtBox1 & "'"
    if not IsNull(txtBox2) then sWhere = sWhere & " and [field2]='" & me.txtBox2 & "'"
    if not IsNull(cboBox) then sWhere = sWhere & " and [field3]='" & me.cboBox & "'"
    
    
    if sWhere = "1=1" then
       me.filterOn = false
    else
       me.filter = sWhere
       me.filterOn = true
    endif

  4. #4
    smaier is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Jul 2020
    Posts
    43
    pbaldy;

    Worked like a charm, thank you so much! I've used the pound sign for dates in queries before. Don't know why it didn't occur to me use them in VBA. Not to excuse my lack of proficiency with usage of quotes. Very much appreciated

    ranman256;

    That's a really creative solution. I'm still working on figuring out my error in adapting it (I'm getting data type mismatch error), but I want to figure it out since it's a different approach than I would have thought of.


    Thank you very much, all!

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!

    To help with ranman's solution, you can use this to see the finished string:

    http://www.baldyweb.com/ImmediateWindow.htm

    The data type mismatch typically means you've used the wrong delimiters. Generally, surround date values with #, text values with ', numeric values with no delimiters.

    Edit: It would also help to see your code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Multiple Drill Down Filter using DoCmd.ApplyFilter
    By sparker75 in forum Programming
    Replies: 1
    Last Post: 07-30-2018, 10:40 AM
  2. Replies: 4
    Last Post: 10-05-2017, 01:15 AM
  3. Multiple entries for ApplyFilter?
    By kwelch in forum Macros
    Replies: 4
    Last Post: 11-25-2016, 06:08 AM
  4. DoCmd.applyfilter for multiple
    By vicsaccess in forum Programming
    Replies: 4
    Last Post: 11-10-2015, 05:02 PM
  5. DoCmd.ApplyFilter with wild card
    By cksm4 in forum Programming
    Replies: 3
    Last Post: 10-05-2010, 08:24 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