MS Access:VBA 组合框

Ms Access: VBA combobox

我在 MS Access 表单中使用以下 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 代码,以便它在过滤时起作用。

由于您的代码在组合的 更新后 事件中,您可以将 SELECT 调整为组合值 --- 仅添加 WHERE当组合值不等于 "(All)" 时的子句。

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