Results 1 to 2 of 2
  1. #1
    jon6035 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Location
    California
    Posts
    12

    Multiple Multi-Select Boxes to Filter Report

    I have a form that utilizes a multi-select list box to filter the report on load.

    Currently I have two, but will add a third, once I figure this problem out.

    When I select a value from both lstPlatoon and lstTrainingType, the report works like a charm. However, the most common response will be to select the "cmdTEEPMultiSelect" without applying a filter to any of the boxes.

    When I do this, I get a syntax error that reads ' AND ' Missing operator.

    I have tried to clear the error, but am stumped. Without the multiple strings in the stLinkCriteria, it handles the no selection quite well.

    Any help would be greatly appreciated.


    Private Sub cmdTEEPMultiSelect_Click()
    On Error GoTo Err_Handler
    'Purpose: Open the report filtered to the items selected in the list box.
    'Author: Allen J Browne, 2004. http://allenbrowne.com
    Dim varItem As Variant 'Selected items
    Dim strWherePLT As String 'String to use as WhereCondition
    Dim strWhereTrainingType As String
    Dim strDescrip As String 'Description of WhereCondition
    Dim lngLen As Long 'Length of string
    Dim strDelim As String 'Delimiter for this field type.
    Dim stDocName As String 'Name of report to open.
    Dim stQueryName As String
    Dim stLinkCriteria As String

    'http://allenbrowne.com/ser-50.html

    'Use design view of query to help determine structure of SQL statement.
    DoCmd.SetWarnings False

    DoCmd.RunSQL ("DELETE temptblCalculatedStartEnd.* FROM temptblCalculatedStartEnd")
    DoCmd.RunSQL ("INSERT INTO temptblCalculatedStartEnd SELECT selCalculatedStartEnd.* FROM selCalculatedStartEnd")
    DoCmd.SetWarnings True



    Select Case Me.frmTEEPLength.Value

    Case 33
    stDocName = "rptTEEP33Days"

    Case 90
    stDocName = "rptTEEP90Days"

    End Select
    'Loop through the ItemsSelected in the list box.

    With Me.lstPlatoon
    For Each varItem In .ItemsSelected
    If Not IsNull(varItem) Then
    'Build up the filter from the bound column (hidden).
    strWherePLT = strWherePLT & strDelim & .ItemData(varItem) & strDelim & ","

    End If
    Next
    End With

    'Remove trailing comma. Add field name, IN operator, and brackets.
    lngLen = Len(strWherePLT) - 1
    If lngLen > 0 Then
    strWherePLT = "[PlatoonLU] IN (" & Left$(strWherePLT, lngLen) & ")"
    lngLen = Len(strDescrip) - 2
    If lngLen > 0 Then
    strDescrip = "Categories: " & Left$(strDescrip, lngLen)
    End If
    End If
    With Me.lstTrainingTypes
    For Each varItem In .ItemsSelected
    If Not IsNull(varItem) Then
    'Build up the filter from the bound column (hidden).
    strWhereTrainingType = strWhereTrainingType & strDelim & .ItemData(varItem) & strDelim & ","
    End If
    Next
    End With

    'Remove trailing comma. Add field name, IN operator, and brackets.
    lngLen = Len(strWhereTrainingType) - 1
    If lngLen > 0 Then
    strWhereTrainingType = "[TrainingTypeTLU] IN (" & Left$(strWhereTrainingType, lngLen) & ")"
    lngLen = Len(strDescrip) - 2
    If lngLen > 0 Then
    strDescrip = "Categories: " & Left$(strDescrip, lngLen)
    End If
    End If

    'Report will not filter if open, so close it. For Access 97, see note 3.
    If CurrentProject.AllReports(stDocName).IsLoaded Then
    DoCmd.Close acReport, stDocName
    End If
    Debug.Print
    'This is where I need help.
    stLinkCriteria = strWherePLT & " AND " & strWhereTrainingType

    If either strWherePLT or strWhereTrainingType is null I get a missing operator error.

    'Omit the last argument for Access 2000 and earlier. See note 4.
    DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
    Exit_Handler:
    Exit Sub
    Err_Handler:
    If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
    MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdPreview_Click"
    End If
    Resume Exit_Handler
    Err_cmdTEEPMultiSelect_Click:

    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Variables strWherePLT and strWhereTrainingType are declared as string type. String type cannot be set to Null. The error should be occurring on the lines where you set those variables. Have you step debugged? Why is there a null in list box?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 3
    Last Post: 06-22-2011, 08:51 AM
  2. 2 multi select list boxes on one search form
    By woodey2002 in forum Forms
    Replies: 2
    Last Post: 11-05-2010, 12:44 PM
  3. Multi-select List Boxes
    By Rawb in forum Programming
    Replies: 6
    Last Post: 09-21-2010, 09:02 AM
  4. Replies: 1
    Last Post: 03-01-2009, 09:53 AM
  5. Replies: 1
    Last Post: 02-25-2009, 07:29 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