Results 1 to 5 of 5
  1. #1
    JimO is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    126

    Incorperate Filters into form code

    I am trying to add a filter into my form code with no success. The simple Select statement works fine but I cannot get it into my code. The statement is:

    Code:
    SELECT * FROM CalendarTbl WHERE Type <> "Pers" OR Type <> "House" OR Type  <> "Gen" ORDER BY EventDay DESC;

    The code right now that works is:
    Code:
    Private Sub RequeryForm()
        Dim W As String
        Dim S As String
       W = ""
        
        If Not IsNull(StartDate) Then
            If W <> "" Then W = W & " AND "
            W = W & "EventDay >= #" & StartDate & "#"
        End If
        If Not IsNull(EndDate) Then
            If W <> "" Then W = W & " AND "
            W = W & "EventDay <= #" & EndDate & "#"
            Me.lblTimer.Caption = "All Items in Date Range"
        End If
        If Done = True Then
            If W <> "" Then W = W & " AND "
            W = W & "Complete = True"
            Me.lblTimer.Caption = "Completed Items in Date Range"
         ElseIf Done = False Then
             If W <> "" Then W = W & " AND "
            W = W & "Complete = False"
            Me.lblTimer.Caption = "Pending Items in Date Range"   
        End If 
        
        S = "Select * FROM CalendarTbl"
        If W <> "" Then S = S & " WHERE " & W
        Me.RecordSource = S & " ORDER BY [EventDay] DESC"
          
    End Sub
    I though I could just modify the WHERE clause to:

    Code:
    If W <> "" Then S = S & " WHERE Type <> 'Pers' OR Type <> 'House' OR Type <> 'Gen' " & W
    But that does not work. I would like to learn how to add this type of filter so any help would be greatly appreciated.

    Thanks



    Jim O

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    instead show ALL records in the query.
    then filter if needed:


    Code:
        Dim W As String
        Dim S As String
       W = ""
        
        If Not IsNull(StartDate) Then
            If W <> "" Then W = W & " AND "
            W = W & "EventDay >= #" & StartDate & "#"
        End If
    
        If Not IsNull(EndDate) Then
            If W <> "" Then W = W & " AND "
            W = W & "EventDay <= #" & EndDate & "#"
            Me.lblTimer.Caption = "All Items in Date Range"
        End If
    
        If Done  Then
            If W <> "" Then W = W & " AND "
            W = W & "Complete = True"
            Me.lblTimer.Caption = "Completed Items in Date Range"
         Else
             If W <> "" Then W = W & " AND "
            W = W & "Complete = False"
            Me.lblTimer.Caption = "Pending Items in Date Range"   
        End If 
        
    
        If W = "" Then
           Me.filterOn =false
        else
           me.filter = W
           Me.filterOn =true
        endif

  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,521
    I'll point out that you have to use AND, not OR. This may also help:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    JimO is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Dec 2015
    Posts
    126
    Thank you both for the response. It appears to work although I don't yet understand it, (at my learning stage I am not sure I know enough to ask the correct questions).

    I will continue to search and try to understand it.

    Again thanks for your time.

    Jim O

  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
    Not knowing what "it" is, hard to know what to explain.

    If you mean the And/Or issue, when you use "not equal to" instead of "equal to", the logic of And vs Or flips. Given your example of:

    WHERE Type <> "Pers" OR Type <> "House" OR Type <> "Gen"

    If the test value is "Pers", it meets the test of <> "House", thus would be included in the result since you said "OR".
    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. filters in continuos form using VBA code
    By TriFith in forum Access
    Replies: 5
    Last Post: 01-11-2017, 04:48 AM
  2. Split Form that Filters
    By QuantifyRisk in forum Forms
    Replies: 12
    Last Post: 11-20-2014, 01:09 PM
  3. Access Form Filters?????
    By Z1nkstar in forum Access
    Replies: 7
    Last Post: 03-14-2014, 02:04 PM
  4. Replies: 7
    Last Post: 11-11-2013, 08:54 AM
  5. Form Filters Help
    By JeffG3209 in forum Forms
    Replies: 1
    Last Post: 05-22-2011, 10:31 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