我在MS Access Form
中使用以下VBA代码进行组合框Private Sub ComboEmployee_AfterUpdate()
Dim myFilter As String
myFilter = "Select * from Filter_Employee where ([LastName] = iif('" & ComboEmployee & "'='(All)',[LastName],'" & ComboEmployee & "'))"
Me.Employee_subform.Form.RecordSource = myFilter
Me.Employee_subform.Form.Requery
End Sub
这很好用,问题是我现在有一个组合框,它在组合框中包含多个列(见图)
如果我使用上面的代码它不起作用..我应该如何调整我的vba代码,以便它在过滤时有效。
答案 0 :(得分:2)
由于您的代码位于组合的 After Update 事件中,因此您可以将SELECT
调整为组合值---只添加WHERE
子句当组合值不等于"(全部)" 时。
Private Sub ComboEmployee_AfterUpdate()
Dim myFilter As String
myFilter = "Select * from Filter_Employee"
If Me.ComboEmployee.Value <> "(ALL)" Then
myFilter = myFilter & " where [LastName] = '" & Me.ComboEmployee.Value & "'"
End If
Debug.Print myFilter '<- view this in Immediate window; Ctrl+g will take you there
Me.Employee_subform.Form.RecordSource = myFilter
End Sub