选中 属性 编译错误

Selected property Compile error

我正在尝试 运行 的这段代码出现编译器错误。显然,选定的 属性 抛出错误:参数不是可选的。有没有更好的方法来做到这一点或我可以使用不同的 属性?

 Private Sub cmbAnalyst_AfterUpdate()

    If cmbAnalyst.Selected = "" Or IsNull(cmbAnalyst.Selected) Then
        Exit Sub
    Else
        whereAtt = whereAtt & " And Analyst = ' cmbAnalyst.Selected '"
        Call queryBuilder
    End If
    End Sub

    Private Sub Form_Load()
    whereAtt = "Select * from tblActionLog where LogID is not null"
    cmbAnalyst.RowSource = "SELECT DISTINCT Analyst FROM tblActionLog"
    Call queryBuilder
    End Sub

    Public Sub queryBuilder()
    testTable.RowSource = whereAtt
    End Sub

您可以使用 ComboBox 的 ListIndex 方法来测试是否正在选择某些内容。您还可以使用 Len() 函数。

Private Sub cmbAnalyst_AfterUpdate()
    If cmbAnalyst.ListIndex <> -1 Then
        whereAtt = whereAtt & " And Analyst = '" & Me.cmbAnalyst &"'"
        Call queryBuilder
    End If
End Sub

我个人会选择 ListIndex。如果没有选择,ListIndex 会 return -1。

来自 Access 帮助主题,ComboBox.Selected 属性 ...

"The Selected property is a zero-based array that contains the selected state of each item in a combo box."

因此 Access 要求的参数是一个索引,它标识该数组的一个成员。

如果您的是一个简单的 select 组合框,您可以检查它的 .Value

'If cmbAnalyst.Selected = "" Or IsNull(cmbAnalyst.Selected) Then
If Len(Trim(Me.cmbAnalyst.Value & vbNullString)) = 0 Then