为什么Access VBA下拉方法不起作用?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么Access VBA下拉方法不起作用?相关的知识,希望对你有一定的参考价值。

我正在使用文本框来过滤Access 2013中的组合框列表。我将过滤器代码放在Form_Timer子项中,以便用户有时间在应用过滤器之前键入整个过滤器字符串,并从中调用计时器文本框更改子。除了一件事之外,它的效果很好:我希望组合框列表下拉并显示结果,它只是不起作用。但是我在组合框的GotFocus子中放了完全相同的代码行,并且该行完美运行。

我还尝试在Change子区域中执行过滤器代码,以防万一有关Form_Timer执行的奇怪之处。结果相同。这是代码:

Private Sub cboCENamesMain_GotFocus()

    Me.cboCENamesMain.Dropdown '<---This line works perfectly.

End Sub

Private Sub Form_Timer()

    Dim strSQL As String

    Me.TimerInterval = 0
    Me.txtFilter.Value = Me.txtFilter.Text
    Me.cboCENamesMain.SetFocus

    strSQL = ""
    strSQL = strSQL & "Select DISTINCT [CE ID] "
    strSQL = strSQL & "From [tblMyTable] "
    If Len(Me.txtFilter) > 0 Then
        strSQL = strSQL & "Where [CE ID] Like ""*" & Me.txtFilter & "*"" "
    End If
    strSQL = strSQL & "Order By [CE ID];"

    Me.cboCENamesMain.RowSource = strSQL

    Me.cboCENamesMain.Dropdown '<---This line doesn't do what it's supposed to.
    Me.txtFilter.SetFocus
    Me.txtFilter.SelStart = Len(Me.txtFilter.Text)
    Me.txtFilter.SelLength = 0

End Sub

Private Sub txtFilter_Change()

    If Len(Me.txtFilter.Text) = 0 _
    Or Len(Me.txtFilter.Text) > 2 Then
        Me.TimerInterval = 500
    End If

End Sub

我可以使用列表框而不是组合框来允许用户查看他们的过滤器输入结果,但这会严重影响我的表单设计。我在谷歌和StackOverflow上搜索过,并没有找到其他人讨论这个问题。有任何想法吗?

答案

这是我的最终代码。这就是我想要的方式。用户键入几个字符,计时器自动将组合框列表过滤到包含键入字符串的项目。

StackOverflow是一个很好的地方来存档这样的东西 - 从一个工作到一个工作,它将节省一些时间重新发明轮子。

代码中的广泛注释适用于在合同结束后需要维护应用程序的分析师。他们都没有写过VBA。

Private Sub cboCENamesMain_Change()

    ' Ordinarily the AfterUpdate event would automatically fire off the Change event.
    ' We set a flag to avoid that - otherwise the list would be filtered to only the selected record.
    If booCancelChange = False Then
        ' Don't bother filtering the combo box list unless the filter text is null or longer than
        ' 1 character.
        If Len(Me.cboCENamesMain.Text) <> 1 Then
            ' Set the Form_Timer event to fire off in 0.3 second to give them time to type a few characters.
            Me.TimerInterval = 300
        End If
    Else
        ' Reset the flag, otherwise the Change code would stop working after the first record selection.
        booCancelChange = False
    End If

End Sub

Private Sub Form_Timer()

    Dim strSQL As String

    ' Reset the timer to not fire off, so that it won't keep running without a change
    ' in the combo box.
    Me.TimerInterval = 0

    ' If they have tabbed out of the combo box after selecting an item, we don't want to
    ' do this.  It's unnecessary and it throws errors from references to the control's
    ' properties when focus is no longer on the control.
    If Screen.ActiveControl.Name = Me.cboCENamesMain.Name Then
        ' Create a SQL filter for the combo box using the entered text.
        strSQL = ""
        strSQL = strSQL & "Select DISTINCT [CE ID] "
        strSQL = strSQL & "From [Covered Entities] "
        If Len(Me.cboCENamesMain.Text) > 0 Then
            strSQL = strSQL & "Where [CE ID] Like ""*" & Me.cboCENamesMain.Text & "*"" "
        End If
        strSQL = strSQL & "Order By [CE ID];"

        ' Apply the filter.
        Me.cboCENamesMain.RowSource = strSQL
        Me.txtRowCount = Me.cboCENamesMain.ListCount
        ' Drop down the combo list so they can see the results of their filter text.
        Me.cboCENamesMain.Dropdown
    End If

End Sub

以上是关于为什么Access VBA下拉方法不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

Access 2010 VBA:为啥这个表单打开和关闭序列不起作用?

使用 Access 2010 数据库的 Excel 2010 VBA DAO 查询不起作用

从 MS Access VBA 通过 ODBC 进行 MySQL 查询:ADODB 异步执行不起作用

xmlnode 代码不起作用 - 访问 vba

访问 2007 VBA DoCmd.Close 不起作用

我希望从 ms-access 2013 数据库中的所有表单、报告和模块中导出 vba 源代码