Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8

    search form

    Hi. I need a little help. I have a form to search by date. I have two input fields: startdate, enddate. THIS IS MY CODE


    Code:
    If Me.Txtstartdate > "" Then
            varWhere = varWhere & "[start_date] >= #" & Day(Me.Txtstartdate.Value) & "/" & Month(Me.Txtstartdate.Value) & "/" & Year(Me.Txtstartdate.Value) & "#"
        End If
        
       
        If Me.Txtenddate > "" Then
            varWhere = varWhere & "[end_date] <= #" & Day(Me.Txtenddate.Value) & "/" & Month(Me.Txtenddate.Value) & "/" & Year(Me.Txtenddate.Value) & "#"
        End If
    works well, except when I enter values for both fields

  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,641
    There would need to be " And " between the two conditions.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    thanks, but this doesn't work
    Code:
    If Me.Txtstartdate > "" Then
            varWhere = varWhere & "[start_date] >= #" & Day(Me.Txtstartdate.Value) & "/" & Month(Me.Txtstartdate.Value) & "/" & Year(Me.Txtstartdate.Value) & "#""AND"
        End If
          
         
        If Me.Txtenddate > "" Then
            varWhere = varWhere & "[end_date] <= #" & Day(Me.Txtenddate.Value) & "/" & Month(Me.Txtenddate.Value) & "/" & Year(Me.Txtenddate.Value) & "#"
        End If
    So where do I put "and"? Sorry, I'm a complete beginner in Access..

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    It would look like this:

    varWhere = varWhere & "[start_date] >= #" & Day(Me.Txtstartdate.Value) & "/" & Month(Me.Txtstartdate.Value) & "/" & Year(Me.Txtstartdate.Value) & "# AND "

    You would have to account for the user only choosing one date.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is an alternative:

    Code:
        If Me.Txtstartdate > "" Then
            varWhere = varWhere & "[start_date] >= #" & Format(Me.Txtstartdate, "dd/mm/yyyy") & "# AND "
        End If
    
        If Me.Txtenddate > "" Then
            varWhere = varWhere & "[end_date] <= #" & Format(Me.Txtenddate, "dd/mm/yyyy") & "#"
        Else
            'end date not entered - remove the last 5 char.... ie " AND "
            varWhere = Left(varWhere, Len(varWhere) - 5)
        End If

  6. #6
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    thank you both.
    Quote Originally Posted by ssanfu View Post
    Here is an alternative:

    Code:
    Else    
        varWhere = Left(varWhere, Len(varWhere) - 5)
    End If
    This is great!

  7. #7
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    Not so great actually. When I try to do my search by other criteria (not date), this line:
    Code:
    varWhere = Left(varWhere, Len(varWhere) - 5)
    causes the error: "Invalid use of null"

  8. #8
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by D R View Post
    Not so great actually. When I try to do my search by other criteria (not date), this line:
    Code:
    varWhere = Left(varWhere, Len(varWhere) - 5)
    causes the error: "Invalid use of null"

    What other criteria??? It works for the tiny bit of code you provided. If there is more code (other criteria), then you will have to modify the code yourself or post it for us to see.

  9. #9
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    Yes.I should have to mention it earlier. Sorry
    Code:
    Private Function BuildFilter() As Variant
        Dim varWhere As Variant
        Dim varType As Variant
        Dim varItem As Variant
        Dim intIndex As Integer
    
        varWhere = Null  
        varType = Null  
        
        
        If Me.TxtNumber > "" Then
            varWhere = varWhere & "[number] LIKE """ & Me.Txtnumber & "*"" AND "
        End If
        
        If Me.Txtsubject > "" Then
            varWhere = varWhere & "[subject] LIKE """ & Me.Txtsubject & "*"" AND "
        End If
          
        If Me.Txtstartdate > "" Then
            varWhere = varWhere & "[start_date] >= #" & Day(Me.Txtstartdate.Value) & "/" & Month(Me.Txtstartdate.Value) & "/" & Year(Me.Txtstartdate.Value) & "# And"
      End If
      
        
        If Me.Txtenddate> "" Then
            varWhere = varWhere & "[end_date] <= #" & Day(Me.Txtenddate.Value) & "/" & Month(Me.Txtenddate.Value) & "/" & Year(Me.Txtenddate.Value) & "#"
      
       Else
       
      varWhere = Left(varWhere, Len(varWhere) - 4)
          End If
        
       
        If Me.Txtsender > "" Then
            varWhere = varWhere & "[sender] LIKE """ & Me.Txtsender & "*"" AND "
        End If
        
     
        If Me.Txtlocation > "" Then
            varWhere = varWhere & "[location] LIKE """ & Me.Txtlocation& "*"" AND "
        End If
        
           For Each varItem In Me.Lsttypes.ItemsSelected
            varType = varType & "[types] = """ & _
                        Me.LstTypes.ItemData(varItem) & """ OR "
            
        Next
        
        
        If IsNull(varType) Then
           
        Else
            
            If Right(varType, 4) = " OR " Then
                varType = Left(varType, Len(varType) - 4)
            End If
     
           
            varWhere = varWhere & "( " & varType & " )"
        End If
        
     
        If IsNull(varWhere) Then
            varWhere = ""
        Else
            varWhere = "WHERE " & varWhere
            
             
            If Right(varWhere, 5) = " AND " Then
                varWhere = Left(varWhere, Len(varWhere) - 5)
            End If
        End If
         
        BuildFilter = varWhere
        
    End Function

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    You don't need that line, as you've already accounted for the " AND " in your code at the end. It would error earlier if nothing had been selected yet.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    I done this way
    Code:
    If IsNull(varWhere) Then
            varWhere = ""
        Else
            varWhere = "WHERE " & varWhere
            
             
            If Right(varWhere, 5) = " AND " Then
                varWhere = Left(varWhere, Len(varWhere) - 5)
            End If
            If Right(varWhere, 6) = "# AND " Then
                varWhere = Left(varWhere, Len(varWhere) - 5)
        End If
         End If
    and now it works. Thanks a lot.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    The second test (with the #) makes no sense. It will never be true, as the test before will have trimmed off the " And ". I would delete that block.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    LOL, you're right. It may be that I made a mistake when I typing this way:
    "# And" and not "# And "
    but it worked with that line

  14. #14
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Does this work?

    Code:
    Private Function BuildFilter() As Variant
        Dim varWhere As Variant
        Dim varType As Variant
        Dim varItem As Variant
        Dim intIndex As Integer
    
        varWhere = Null
        varType = Null
    
        If Me.Txtnumber > "" Then
            varWhere = varWhere & "[number] LIKE """ & Me.Txtnumber & "*"" AND "
        End If
    
        If Me.Txtsubject > "" Then
            varWhere = varWhere & "[subject] LIKE """ & Me.Txtsubject & "*"" AND "
        End If
    
        If Me.Txtstartdate > "" Then
    '          I would use:    Format(Me.Txtstartdate, "dd/mm/yyyy")
            varWhere = varWhere & "[start_date] >= #" & Day(Me.Txtstartdate.Value) & "/" & Month(Me.Txtstartdate.Value) & "/" & Year(Me.Txtstartdate.Value) & "# AND "
        End If
    
        If Me.Txtenddate > "" Then
            varWhere = varWhere & "[end_date] <= #" & Day(Me.Txtenddate.Value) & "/" & Month(Me.Txtenddate.Value) & "/" & Year(Me.Txtenddate.Value) & "# AND "
        End If
    
        If Me.Txtsender > "" Then
            varWhere = varWhere & "[sender] LIKE """ & Me.Txtsender & "*"" AND "
        End If
    
        If Me.Txtlocation > "" Then
            varWhere = varWhere & "[location] LIKE """ & Me.Txtlocation & "*"" AND "
        End If
    
        For Each varItem In Me.LstTypes.ItemsSelected
            varType = varType & "[types] = """ & Me.LstTypes.ItemData(varItem) & """  OR "
        Next
    
        If Not IsNull(varType) Then
            If Right(varType, 4) = " OR " Then
                varType = Left(varType, Len(varType) - 4)
            End If
            varWhere = varWhere & "( " & varType & " )"
        End If
    
        If IsNull(varWhere) Then
            varWhere = ""
        Else
            varWhere = "WHERE " & varWhere
    
            If Right(varWhere, 5) = " AND " Then
                varWhere = Left(varWhere, Len(varWhere) - 5)
            End If
        End If
    
        BuildFilter = varWhere
    
    End Function

  15. #15
    D R is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    8
    yes, it works just fine

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Search Form
    By whojstall11 in forum Forms
    Replies: 28
    Last Post: 07-06-2012, 08:27 AM
  2. Search Form Help
    By carguy37757 in forum Forms
    Replies: 4
    Last Post: 09-16-2011, 11:23 AM
  3. Replies: 9
    Last Post: 02-15-2011, 03:05 PM
  4. Search box on a form.
    By annaisakiwi in forum Forms
    Replies: 3
    Last Post: 01-02-2011, 08:39 PM
  5. Search on in a form
    By newtoAccess in forum Access
    Replies: 2
    Last Post: 12-08-2010, 11:28 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