For Each in listbox not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Selesti
    New Member
    • Sep 2006
    • 10

    For Each in listbox not working

    I've created a form dialog box with several checkboxes to determine which offices to include in a sales report. However, I'm getting an "Object doesn't support this property or method" error from MS Access, which isn't very helpful as it doesn't let me debug or pinpoint what's going wrong. I -think- it's at this line

    For Each VarItem in Me.lstOffices
    strOffices = strOffices & ",'" & Me.lstOffices.I temData(varItem ) & "'"
    Next varItem

    where Me.lstOffices is a hidden, blank listbox on the form that gets items added to it based on which offices are checked. Here's the full code below. I know an alternate method of doing this is to use a multiple select listbox instead of checkboxes, but my supervisor seems set on checkboxes.

    Code:
        Dim strDocName As String
        Dim varItem As Variant
        Dim strOffices As String
        Dim strStatus As String
        Dim strFilter As String
        
        'Clear listbox contents.
        Me.lstOffices.RowSource = ""
        
        If chkLA Then
            Me.lstOffices.AddItem 1
        End If
            
        If ChkSF Then
            Me.lstOffices.AddItem 2
        End If
            
        If chkLV Then
            Me.lstOffices.AddItem 3
        End If
            
        If chkChi Then
            Me.lstOffices.AddItem 4
        End If
    
        For Each varItem In Me.lstOffices
            strOffices = strOffices & ",'" & Me.lstOffices.ItemData(varItem) & "'"
        Next varItem
    
    'Build filter string
    strFilter = "[SalesOffice] " & strOffice & ""
    
    ' Apply the filter and switch it on
        With Reports![rptPreJobList]
            .Filter = strFilter
            .FilterOn = True
        End With
        
        If Len(strOffices) = 0 Then
            strOffices = "Like '*'"
        Else
            strOffices = Right(strOffices, Len(strOffices) - 1)
            strOffices = "IN(" & strOffices & ")"
        End If
    
        strDocName = "rptPreJobList"
        DoCmd.OpenReport stDocName, acPreview
  • Selesti
    New Member
    • Sep 2006
    • 10

    #2
    Never mind. I figured it out; I was making it way too complicated with the list box - I took it out and just added the numbers to the string. for anyone who is curious, here is my ending code.

    Code:
        'Clear listbox contents.
        Me.lstOffices.RowSource = ""
        
        If chkLA Then
            strOffices = strOffices & ", 1"
        End If
            
        If ChkSF Then
            strOffices = strOffices & ", 2"
        End If
            
        If chkLV Then
            strOffices = strOffices & ", 3"
        End If
            
        If chkChi Then
            strOffices = strOffices & ", 4"
        End If
        
        If Len(strOffices) = 0 Then
            strOffices = "Like '*'"
        Else
            strOffices = Right(strOffices, Len(strOffices) - 1)
            strOffices = "IN(" & strOffices & ")"
        End If
        
        
    ' Build filter string
    strFilter = "[SalesOffice] " & strOffices
    
    ' Open report
        stDocName = "rptPreJobList"
        DoCmd.OpenReport stDocName, acPreview
      
    
    ' Apply the filter and switch it on
        With Reports![rptPreJobList]
            .Filter = strFilter
            .FilterOn = True
        End With

    Comment

    Working...