Results 1 to 6 of 6
  1. #1
    bnar is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    12

    Search Form using Vb


    Hi Friends

    i have a database with a search form created using Vb code..query name is (qrySearch) and the form name is (frmSearch) search works fine except for OrderDate field which i want to give it 2 dates and query returns data between those 2 dates..any help please
    Attached Files Attached Files

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Are you saying you want to add Code to Function BuildFilter() so that it will return data between textBeforeDate and textAfterDate?

    Wait . . . I just noticed that you have the code in there - but it's probably not working - right?
    That's because when you work with dates - you have to use "#" on both sides of the date in order for Access to recognize it as a Date.

  3. #3
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Try something like this:
    Code:
        'Check for Start Date
        If Not IsNull(Me.txtAfterDate) Then ' 
            varWhere = varWhere & "[OrderDate] Between #" & Me.txtAfterDate & "# AND "
        End If
        
        'Check for End Date
        If Not IsNull(Me.textBeforeDate) Then 
            varWhere = varWhere & "#" & Me.textBeforeDate & "#" & " AND "
        End If

  4. #4
    bnar is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    12
    Click image for larger version. 

Name:	error3075.png 
Views:	7 
Size:	64.5 KB 
ID:	7965 tryed you code it gives this error when tyoing twi dates,,also other search fields does not work.


    but i found this code works perfectly...(if you have time to make your code work also it will better)Thanks

    Code:
    If Me.txtAfterDate > "" Then
    '          I would use:    Format(Me.Txtstartdate, "dd/mm/yyyy")
            varWhere = varWhere & "[orderdate] >= #" & Month(Me.txtAfterDate.Value) & "/" & Day(Me.txtAfterDate.Value) & "/" & Year(Me.txtAfterDate.Value) & "# AND "
        End If
     
        If Me.textBeforeDate > "" Then
            varWhere = varWhere & "[orderdate] <= #" & Month(Me.textBeforeDate.Value) & "/" & Day(Me.textBeforeDate.Value) & "/" & Year(Me.textBeforeDate.Value) & "# AND "
        End IfIf Me.txtAfterDate > "" Then
    '          I would use:    Format(Me.Txtstartdate, "dd/mm/yyyy")
            varWhere = varWhere & "[orderdate] >= #" & Month(Me.txtAfterDate.Value) & "/" & Day(Me.txtAfterDate.Value) & "/" & Year(Me.txtAfterDate.Value) & "# AND "
        End If
     
        If Me.textBeforeDate > "" Then
            varWhere = varWhere & "[orderdate] <= #" & Month(Me.textBeforeDate.Value) & "/" & Day(Me.textBeforeDate.Value) & "/" & Year(Me.textBeforeDate.Value) & "# AND "
        End If

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    "Value" is not required - it is the default property (saves on typing)

    You have to remove the last "AND" in the WHERE string:

    Code:
       If IsDate(Me.txtAfterDate) Then
    
          varWhere = varWhere & "[orderdate] >= #" & Me.txtAfterDate & "# AND "
       End If
    
       If IsDate(Me.textBeforeDate) Then
          varWhere = varWhere & "[orderdate] <= #" & Me.textBeforeDate & "# AND "
       End If
    
       ' remove the last AND
       If Len(Trim(varWhere)) > 0 Then
          varWhere = Left(varWhere, Len(varWhere) - 5)
       End If
    
       'for debugging
       MsgBox varWhere

  6. #6
    bnar is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2012
    Posts
    12
    Thanks a lote ssanfu it works great.
    Sorry about mistake in my previous post, its pasted twice it's just one statement for each (
    Me.txtAfterDate And Me.textBeforeDate)

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

Similar Threads

  1. Replies: 1
    Last Post: 04-20-2012, 03:16 AM
  2. Replies: 12
    Last Post: 03-22-2012, 02:48 AM
  3. Help with search form
    By PoorCadaver in forum Access
    Replies: 18
    Last Post: 12-17-2011, 01:19 AM
  4. Replies: 7
    Last Post: 11-16-2011, 01:22 PM
  5. Replies: 9
    Last Post: 02-15-2011, 03:05 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