使用过滤器后,列表框无法选择

时间:2018-01-30 20:56:45

标签: vba ms-access access-vba access

我打算编写一个代码,从Employees列表框中选择员工,并将其添加到指定的列表中。我有两个列表框,其中一个是根据项目编号填充的,右边一个显示所有员工。当我使用任何过滤器时,员工列表框(右侧)变得无法选择,我无法定义我想要管理的员工。到目前为止,我使用了这段代码:

Private Sub FilterEmployees()
Dim strListSource As String

strListSource = "SELECT tblLU_Employee.EmployeeID, StrConv([tblLU_Employee].[FirstName],3) AS [First Name], StrConv([tblLU_Employee].[LastName],3) AS [Last Name] FROM tblLU_Employee"
If CityFilt = "" And StatusFilt = "" Then
    'no change
ElseIf CityFilt <> "" And StatusFilt <> "" Then
     strListSource = strListSource & " WHERE tblLU_Employee.Office = '" & CityFilt.Value & "'"
     strListSource = strListSource & " AND tblLU_Employee.Status = '" & StatusFilt & "'"
ElseIf CityFilt <> "" Then
     strListSource = strListSource & " WHERE tblLU_Employee.Office = '" & CityFilt.Value & "'"
ElseIf StatusFilt <> "" Then
     strListSource = strListSource & " WHERE tblLU_Employee.Status = '" & StatusFilt & "'"
End If
strListSource = strListSource & " ORDER BY tblLU_Employee.LastName"

NonAssignedList.RowSource = strListSource
Me.AllowEdits = True
NonAssignedList.AllowValueListEdits = True

如果我不使用过滤器,我可以选择我想要的员工,但如果我使用过滤器我就不能。请检查图像: Pop-up Form

1 个答案:

答案 0 :(得分:0)

有些事情值得尝试:

  1. 将此行添加到您的函数中:NonAssignedList.Requery
  2. 如果这不起作用,而不是让所有代码重新编写列表框的查询,只需使查询始终指向过滤器(如果它为空,则使用ISNULL检查)(见下文)
  3. 查询:

    SELECT tblLU_Employee.EmployeeID, StrConv([tblLU_Employee].[FirstName],3) AS [First Name], 
    StrConv([tblLU_Employee].[LastName],3) AS [Last Name] FROM tblLU_Employee
    WHERE 
        iif(ISNULL(Forms!YourFormNameHere!CityFilt), 1=1, 
            tblLU_Employee.Office = "'" & Forms!YourFormNameHere!CityFilt & "'")
        AND
        iif(ISNULL(Forms!YourFormNameHere!StatusFilt), 1=1, 
            tblLU_Employee.Status = "'" & Forms!YourFormNameHere!StatusFilt & "'")
    

    然后,只要过滤器下拉列表中的任何一个随NonassignedList.Requery变化,就可以触发重新查询