Access女士:VBA组合框

时间:2015-10-20 13:28:53

标签: vba ms-access combobox access-vba ms-access-2010

我在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

这很好用,问题是我现在有一个组合框,它在组合框中包含多个列(见图)enter image description here

如果我使用上面的代码它不起作用..我应该如何调整我的vba代码,以便它在过滤时有效。

1 个答案:

答案 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