Hello,
I have a form that contains 2 multi select listboxes. The form opens a report filtered based on selections from the listboxes. I am trying to use a function by Duane Hookum that I found online but I am getting an error message: 2491: The action or method is invalid because the form or report isn’t bound to a table or query. The function is pulling the correct selections from the listbox according to the msgbox. The error occurs in this line of code: DoCmd.ApplyFilter Task. Any help would be greatly appreciated!
Function:
Option Compare Database
Option Explicit
Public Function BuildIn(lboListBox As ListBox, _
strFieldName As String, strDelim As String) As String
'send in
' List box control object
' Name of the field
' Delimiter which is typically a single quote for text
Dim strIn As String
Dim varItem As Variant
If lboListBox.ItemsSelected.Count > 0 Then
strIn = " AND " & strFieldName & " In ("
For Each varItem In lboListBox.ItemsSelected
strIn = strIn & strDelim & lboListBox.ItemData(varItem) & strDelim & ", "
Next
'remove the last ", " and add the ")"
strIn = Left(strIn, Len(strIn) - 2) & ") "
End If
BuildIn = strIn
End Function
Command Button Code:
Private Sub OK_Click()
DoCmd.SetWarnings False
Dim strSearch As String
Dim Task As String
strSearch = "1 = 1 "
' this code assumes both Model and Color are text/strings
strSearch = strSearch & BuildIn(Me.lstProduct, "QProductCode", "'")
strSearch = strSearch & BuildIn(Me.lstFunction, "QFA_Name", "'")
MsgBox (strSearch)
Task = "select * from qryRpt_ByProductMainDetail_Select where " & strSearch
DoCmd.ApplyFilter Task
DoCmd.OpenReport "rptChecklists_By_Product", acViewPreview, , strSearch
End Sub
I have a form that contains 2 multi select listboxes. The form opens a report filtered based on selections from the listboxes. I am trying to use a function by Duane Hookum that I found online but I am getting an error message: 2491: The action or method is invalid because the form or report isn’t bound to a table or query. The function is pulling the correct selections from the listbox according to the msgbox. The error occurs in this line of code: DoCmd.ApplyFilter Task. Any help would be greatly appreciated!
Function:
Option Compare Database
Option Explicit
Public Function BuildIn(lboListBox As ListBox, _
strFieldName As String, strDelim As String) As String
'send in
' List box control object
' Name of the field
' Delimiter which is typically a single quote for text
Dim strIn As String
Dim varItem As Variant
If lboListBox.ItemsSelected.Count > 0 Then
strIn = " AND " & strFieldName & " In ("
For Each varItem In lboListBox.ItemsSelected
strIn = strIn & strDelim & lboListBox.ItemData(varItem) & strDelim & ", "
Next
'remove the last ", " and add the ")"
strIn = Left(strIn, Len(strIn) - 2) & ") "
End If
BuildIn = strIn
End Function
Command Button Code:
Private Sub OK_Click()
DoCmd.SetWarnings False
Dim strSearch As String
Dim Task As String
strSearch = "1 = 1 "
' this code assumes both Model and Color are text/strings
strSearch = strSearch & BuildIn(Me.lstProduct, "QProductCode", "'")
strSearch = strSearch & BuildIn(Me.lstFunction, "QFA_Name", "'")
MsgBox (strSearch)
Task = "select * from qryRpt_ByProductMainDetail_Select where " & strSearch
DoCmd.ApplyFilter Task
DoCmd.OpenReport "rptChecklists_By_Product", acViewPreview, , strSearch
End Sub