Results 1 to 14 of 14
  1. #1
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52

    Question Filter Not Working

    I am using a form so that the user can enter a date range. That date range is then captured and is supposed to be used to filter the data in a query that is pulling the needed fields from a large main table in the database. The query results that are in that date range should be the only records in the report, but right now the filter does not appear to be working because all of the records from my query are showing up in the report.

    Code:
    Private Sub cmdPreview_Click()
        On Error GoTo Err_Handler      
     
        Dim strReport As String
        Dim strDateField As String
        Dim strWhere As String
        Dim reportView As Long
        Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Do NOT change it to match your local settings.
        
        'DO set the values in the next 3 lines.
        strReport = "Monthly Production Summary"      'Put your report name in these quotes.
        strDateField = "[MFG COMP]" 'Put your field name in the square brackets in these quotes.
        lngView = acViewPreview     'Use acViewNormal to print instead of preview.
        
        'Build the filter string.
        If IsDate(Me.txtStartDate) Then
            strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
        End If
        If IsDate(Me.txtEndDate) Then
            If strWhere <> vbNullString Then
                strWhere = strWhere & " AND "
            End If
            strWhere = strWhere & "(" & strDateField & " <= " & Format(Me.txtEndDate, strcJetDate) & ")"
        End If
        
        'Close the report if already open: otherwise it won't filter properly.
        If CurrentProject.AllReports(strReport).IsLoaded Then
            DoCmd.Close acReport, strReport
        End If
        
        'Open the report.
        'Debug.Print strWhere        'Remove the single quote from the start of this line for debugging purposes.
        DoCmd.OpenReport strReport, lngView, strWhere
    Exit_Handler:
        Exit Sub
    Err_Handler:
        If Err.Number <> 2501 Then
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
        End If
        Resume Exit_Handler
    End Sub


  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Start by moving your argument to the WhereCondition argument slot which is the 4th parameter.
    DoCmd.OpenReport strReport, lngView, , strWhere

  3. #3
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52
    When I do that I get Run-Time Error: 30025 'Invalid SQL Statement. Check the Server filter on the form record source. when it tries to execute the openreport command

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Add a MsgBox "[" & strWhere & "]" just before you run the report and look at the string for validity.

  5. #5
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52
    I get in the message box. [([MFG COMP] >= #05/01/2011#) AND ([MFG COMP] <= #05/31/2011#)]

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    That looks like a valid WhereCondition argument. Shorten it to just the >= part and see if it still barfs.

  7. #7
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52
    I still get the same error even after commenting out the second half of the wherecondition

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Change it to absolute [MFG COMP] = #05/01/2011#
    Is the [MFG COMP] field a DateTime field or a text field?

  9. #9
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52
    It is a datetime field and I tried making it an absolute and I got the same error, but in the end I still need it to be a date range I can't use absolutes

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I'm not sure what you are doing. Can you copy and paste the OpenReport line you are using so we can see it?

  11. #11
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52
    DoCmd.OpenReport strReport, lngView, , strWhere

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try opening the Report in Design view and see if anything strange is in the Filter property. See if the Report runs by itself.

  13. #13
    BigCat's Avatar
    BigCat is offline Vice Pres. of Napping
    Windows XP Access 2007
    Join Date
    May 2011
    Location
    Box Canyon
    Posts
    52
    I made that change and it opens in design view and then if I switch over to report view nothing has been filtered it has every record from my query in it, and I just want records from the month given in the date range

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Any chance you can post your db zipped up? We're going around in circles. Do a Compact and Repair first and remove any sensitive data.

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

Similar Threads

  1. Replies: 2
    Last Post: 08-18-2011, 10:20 PM
  2. Slow response over VPN or To Filter or not To Filter
    By rcrobman in forum Database Design
    Replies: 0
    Last Post: 04-30-2011, 02:37 PM
  3. Filter command not working for subform
    By yes sir in forum Access
    Replies: 15
    Last Post: 10-15-2010, 10:06 PM
  4. Filter By Form not working!
    By Freybourne in forum Access
    Replies: 6
    Last Post: 06-22-2010, 09:41 PM
  5. Working with PDF's
    By Mitch87 in forum Access
    Replies: 1
    Last Post: 02-19-2010, 11: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